Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Created October 12, 2013 15:28
Show Gist options
  • Save ibanezmatt13/6951257 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/6951257 to your computer and use it in GitHub Desktop.
[15:59] <ibanezmatt13_> internet failed
[16:00] <mfa298> the bits I said are:
[16:00] <mfa298> the TODO note is there as I know it's making assumptions
[16:00] <mfa298> the way it's written it will only work if the first two digits are degrees and the rest is minutes
[16:00] <ibanezmatt13_> oh
[16:01] <ibanezmatt13_> does the ublox always return the lat in the same format?
[16:02] <mfa298> that's the assumption I'm making
[16:02] <mfa298> although it's something I'll probably fix sometime
[16:03] <ibanezmatt13_> I think I personally would like to make a pretty bomb-proof function
[16:03] <ibanezmatt13_> problem is that all the examples I have seen a far too complex for me to understand
[16:04] <mfa298> I think at that point I wanted to get something that was mostly working with the intention of going back to it
[16:04] <ibanezmatt13_> when you put it like that, that's true for me as well
[16:04] <mfa298> I may not be doing it in the best way but it's a conversion that seems to work.
[16:05] <ibanezmatt13_> ok, I think a good start would be to look at the conversion function I used in Python and see how that worked and then I can just try and translate that into C, because that worked for me
[16:05] <ibanezmatt13_> https://github.com/ibanezmatt13/NORB/blob/master/NORB.py Line 154
[16:05] <ibanezmatt13_> it takes the same parameters as your function
[16:05] <ibanezmatt13_> but the code isn't very self-documenting I'm afraid so variables have stupid names
[16:06] <mfa298> if you're trying to understand how my function works the key is working out what's happening in: result+=(lat[0]-'0')*10;
[16:06] <ibanezmatt13_> yeah, I don't get that bit :)
[16:07] <mfa298> break it down into parts, firstly what's lat[0]
[16:07] <ibanezmatt13_> the first character element of the character pointer you inputted?
[16:07] <mfa298> yep
[16:08] <ibanezmatt13_> what's hte -'0' bit for?
[16:08] <ibanezmatt13_> the*
[16:08] <mfa298> do you know what the '0' is
[16:08] <ibanezmatt13_> not really
[16:09] <mfa298> its defining a character (like " " is used for a string.
[16:09] <ibanezmatt13_> still not 100% sure what it's doing
[16:09] <mfa298> you might have come accross using a test of '\n' before as a test for a newline which is doing the same thing
[16:10] <mfa298> the important part to remeber is that a single character is just a byte of data in ascii format
[16:10] <mfa298> so you can also treat them like a number
[16:10] <ibanezmatt13_> I understand that bit, but still can't see what it's there for :/
[16:11] <mfa298> so for your example string of 4808.668, lat[0] will be '4'
[16:11] <ibanezmatt13_> yes
[16:12] <mfa298> that's got an ascii value of 52
[16:12] <ibanezmatt13_> right
[16:12] <mfa298> '0' has an ascii value of 48
[16:12] <ibanezmatt13_> (52-48) * 10
[16:12] <ibanezmatt13_> = 40
[16:12] <mfa298> so '4' - '0' is the same as 52-48
[16:13] <mfa298> yep
[16:13] <ibanezmatt13_> result = result + 40 which in the first instance = 40
[16:13] <mfa298> and then the line after does the same but without the *10 so '8'-'0' = 56-48 = 8
[16:14] <ibanezmatt13_> so result is currently 48
[16:14] <mfa298> yep
[16:14] <ibanezmatt13_> this is starting to make HUGE ammounts of sense
[16:14] <ibanezmatt13_> almost there
[16:15] <ibanezmatt13_> now then
[16:15] <ibanezmatt13_> result+=(strtof(&lat[2],NULL) /60);
[16:15] <ibanezmatt13_> the final piece of this jigsaw
[16:15] <ibanezmatt13_> so this is adding something to the 48 we have
[16:15] <mfa298> from what you know of pointers what's &lat[2] going to give
[16:15] <ibanezmatt13_> the third element of the address for the latitude variable?
[16:16] <ibanezmatt13_> wait
[16:16] <ibanezmatt13_> no, the third number result
[16:16] <ibanezmatt13_> would make more sense, but I thought & meant address
[16:16] <mfa298> you were close
[16:17] <ibanezmatt13_> that's good
[16:17] <mfa298> lat[2] would be the third item in the string
[16:17] <ibanezmatt13_> yes
[16:17] <ibanezmatt13_> aaah
[16:17] <mfa298> &lat[2] is then the address of the third item
[16:17] <ibanezmatt13_> the address of the third item of the string
[16:17] <ibanezmatt13_> yes
[16:17] <ibanezmatt13_> got it
[16:18] <ibanezmatt13_> makes sense
[16:18] <ibanezmatt13_> let me remember what strtof does
[16:18] <mfa298> if you did a printf("%s", &lat[2]) you'de get 08.668
[16:18] <ibanezmatt13_> of course
[16:18] <ibanezmatt13_> so it converts it to float
[16:19] <mfa298> yes
[16:19] <ibanezmatt13_> what's going on with the NULL and the /60 part?
[16:19] <ibanezmatt13_> sadf
[16:19] <ibanezmatt13_> oops
[16:19] <ibanezmatt13_> d
[16:19] <mfa298> I think the NULL is a space to put anything strtof can't convert
[16:20] <ibanezmatt13_> ah
[16:20] <mfa298> so you go from result+=(strtof(&lat[2],NULL) /60);
[16:20] <ibanezmatt13_> 48.668
[16:20] <mfa298> to result+=(strtof("08.668",NULL) /60);
[16:20] <ibanezmatt13_> the /60 gets it from hours to minutes (or minutes to seconds) i guess
[16:20] <mfa298> to result+=(08.668 / 60) ;
[16:21] <ibanezmatt13_> and there you have it! Decimal degrees :D
[16:21] <ibanezmatt13_> well that wasn't so bad
[16:22] <ibanezmatt13_> thankfully I won't be crossing any meridians just yet so this can stay simple for now
[16:22] <ibanezmatt13_> now for the very final step...
[16:22] <ibanezmatt13_> what will this function return?
[16:22] <mfa298> dealing with the equator / meridian is easy, the value just becomes negative
[16:23] <ibanezmatt13_> A number to tell the caller whether all is good or not, or the new latitude...
[16:23] <ibanezmatt13_> oh wait, we're doing this with pointers aren't we
[16:23] <mfa298> pointers is the way to go to do it properly
[16:23] <ibanezmatt13_> So the function will return for instance, -1 if it failed. Then the caller knows not to retrieve the data stored in new_latitude
[16:24] <ibanezmatt13_> but if it returns a '1', it knows the conversion worked so it continues as it was and gets the data from the address new_latitude?
[16:24] <mfa298> that's pretty much it
[16:25] <ibanezmatt13_> You can't begin to imagine how pleased I am that I understand this! :)#
[16:25] <mfa298> although if the function has failed the key is more don't use the data as it might be valid
[16:25] <ibanezmatt13_> ah yes
[16:25] <ibanezmatt13_> right, I'm gonna go have a go at making this function work
[16:25] <ibanezmatt13_> thanks for the help, as always very much appreciated
[16:26] <mfa298> hopefully as you're stating to find once you've got pointers they're really useful. they just look very complex until it clicks
[16:27] <ibanezmatt13_> I think it has definitely clicked
[16:27] <ibanezmatt13_> or at least partly clicked which is good enough for now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment