sunnuntai 24. helmikuuta 2019

Nerf scope


Some time ago kid was given a large Nerf-type rifle. It was not an actual nerf, I think, but a clone or facsimile of one. No matter, ammo is compatible both ways and he is happy with it so I don't really care.

This gun had kind of "red dot" scope (cross actually when viewed through the scope), claiming infrared sensing. Which I dismissed as obviously fake, of course. But it did seem to do something, nevertheless, changing color of the cross based on some parameters I couldn't really figure out with brief check.

And then it ceased to function. If you have read previous posts, you know what this means: open it and see what's inside!


It was closed with just some screws, so it opened up nicely. And the problem is already obvious here; plus wire (red one) had come loose from battery pack (4 AAs). Easy fix.

But otherwise the thing was way more complex than I anticipated. Here the thing is upside down, but starting from top of the image, the things are: Battery pack, gun attachment (spring-loaded "click"), view finder (with one tilted "glass" where cross is projected, and then, from left; controls (on/off toggle, mode; PCB itself, and some vaguely lens-looking thingamajic, although it is kinda opaque to visible light).


Closer look to PCB which actually has surprising amount of stuff for a cheap toy. I didn't take the optics on other side off, but whatever's in there seems to be connected with four pins, in center, so I don't think it's a real camera - or at least high-resolution one. Below the contacts of yellow wires seems to be some voltage regulation (I think), and on left main MCU.

On top it the LED and its optics for projecting the cross to viewfinder.

Piece on right is control part; on/off toggle (switch with wires) and two buttons through front panel to PCB.

Unfortunately I was in a lots of hurry back then and didn't bother finding out more about this thing, and just fixed it and put it back together. I didn't even see what type of chip the controller/MCU is! I didn't really expect this kind of complexity, really.

Maybe next time I'll check further.





perjantai 8. helmikuuta 2019

Corrupt debug data


I work mostly on embedded stuff, in a more "classical" sense that the current trend is. That is, I work on "bare iron", with no OS between my program and the chip. That is how I've done things and that is how I still do things - although in some cases something like Raspberry PI core with custom PCB with application-specific stuff placed on it is a better choice. And sometimes feature creep makes things to go sideways; where original design was easy to do with fully custom board with some tiny microcontroller, added requirements add more and more weigh there, to degree where it would have been better to use RPI to begin with. Unfortunately that would have required full redesign which was just not possible due to things I won't go into here.

But that was not what I was going to write about anyway, so disregard that. I was talking about bare iron stuff. When I was getting started, 20 years ago now, in-circuit debuggers/emulators were crazy expensive and I couldn't afford them. So for debugging I used the next best thing: debug output. System prints out debug statements that are part of code, to be removed from release build.

First designs often had just serial port, often due to limited MCU I was using, so this resulted weird and sometimes even twisted systems where serial port was shared between main application and debug stuff. Good times, good times.

Things are very different now, but I still don't like debuggers with my embedded stuff, still preferring debug prints, these days with dedicated serial port that isn't just populated on production boards.

This of course makes crashes sometimes hard to trace down, so I've developed some tools I've mentioned before for that. Now I've got also network-enabled logger where latest debug data is stored on device in case of issues and sent out with WLAN. Methods have become more sophisticated, but idea is still same; when there is a problem, I use my psychic debugging powers (thanks for Raymond Chen for that term) to make my initial  guess on what the problem might be - often with very limited information the (very often non-technical, to put it mildly) customer tells me and throw debug outs in there. More often than not my guesses end up being correct and issue is found and fixed. Granted, sometimes debug prints aren't even needed as issue can be found just by reading the code and comparing it against reported symptoms.

But sometimes this system fails me, like just now.

I was working with a new product, making some relatively small changes around application codebase (at the moment weighing some 37kloc - not really that much for a system with full-color graphic display, touch screen and WLAN, Bluetooth and GPS in it - and remember, no OS or libraries to take care of those either) when I suddenly noticed my debug prints being corrupt. Messages seemed to be half of message A, half of message B and sometimes part of them missing completely.

I write in C. You can guess what my initial guess was. Memory corruption, must be. Some change I made soiled itself and everything around it - including debug data buffer where it is stored briefly before pushed out through serial port.

I spent full hour looking for this issue, to no avail. No matter I did, no matter which parts of application I disabled (trying to isolate the issue), problem remained.

Eventually I got a vague hunch and unplugged the USB-RS232 converter I've been using and plugged it back in. Problem fixed, no more corruption.

Now that I got that figured out I do think that I've run into this issue once before, but as much as I try, I can't recall what exactly happened back then, so this hunch may have been more of recollection.

What makes this curious is that I've been using these same USB-RS232 converters for more than 10 years now, with notable lack of this kind of issues. This very specific converted I've had almost from start - I recognize it due to red tape I've wrapped around it (because I often have three or four of these on desk, all plugged in computer - how you recognize correct one without some kind of marking?).

Only variable thing here is that this computer here is running Windows 10, with its built-in USB-RS232 drivers, and my main work computer still has Windows 7 on it with manufacturer-provided drivers. Makes me wonder about newer systems reliability, just a little bit.




sunnuntai 3. helmikuuta 2019

How to read GPS


If you don't have fancy operating system or API sitting between you and your GPS module, you very likely have to deal with NMEA data sooner or later to get your location. For more complete reference, this page has quite nice list of move common data packages your GPS may send to you.

By default most receivers send out much more than you need, but the extra messages can be turned off or ignored. Typically GPRMC is all you need to get your location.

It will look like this (copy from above page).

$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
 

Or possibly like this:

$GPRMC,180633.059,A,6543.8270,N,02631.0022,E,2.20,182.21,030219,,,A*62
 

The format depends on device used and is kinda historical artifact, as working with it is kinda painful.

First thing you want is validity field, second after header, here A. A means that GPS fix is valid and rest of the data is valid. V means invalid, so ignore messages with that flag.

Then comes latitude in a somewhat weird format: 6543.8270,N . This actually means 65 degrees and 43.8270 minutes north. Minutes go from 0.000 to 59.9999 and back to zero, so if you want to do anything else with this data than just to store it, you pretty much will have to convert this figure to decimal degrees first. And of course same applies to longitude. Not difficult, but this may come as a nasty surprise if you are just getting started with GPS.

Note also that number of decimals on those message examples are different. If you are working with floating point numbers, this isn't too much of a trouble, but if you want to do this with integers, it gets a bit messier as you have to take care to keep number of decimals the same when processing the data. Fortunately it seems that at least this is same within a device; GPS receivers don't suddenly change from, say, 2 decimal places to 4.

So there you go, quick primer on finding yourself with a help of shiny new GPS module!