I’ve taken the existing code from this project, and very happy with it so far.
I’m now however in a position where I need to make use of some third-party sensors which I’ve purchased from hitechnic, such as an accelerometer, a gyroscope, and a 3D compass – to mention a few.
I’m not sure where to start now, but what I need to do is add to my existing code base (which is based on the this), and effectively glue my framework to the new hardware.
Can anyone point me in the right direction? I can’t find any APIs from the device manufacturer, (but I have emailed them and asked – no reply yet).
I’ve also started to document my findings on this page.
Okay, I’ve had a look. The analog sensors, such as the Gyro, are dead easy…
I pretty much just reused that of another analog sensor – the Light sensor…
For polling, I used the generic polling method…
…from the LegoNXTRemote code.
The digital ones are not as easy – specially to someone who has zero sw/hw experience. Here is the working ultrasonic sensor code, setup, and for polling. I will only write the prototype of these methods and a git clone for those interested in the full code at the end.
Note how it has it’s own dedicated method for polling. So, now the question is how to write one for the accelerometer.
The information you get when buying the sensor is a table mapping addresses to content:
…looking at the ultrasonic sensor, I can see references to
0x42– which I am guessing is where the address goes, but that’s all i can guess right now.I’ll let you know if I get any progress on this.
Okay, here’s where it’s at with the Accelerometer.
I first send the device the following message…
What that means respectively (I’m could well be wrong) is…
Next we send a read request…
And to that we get a response…
…and that ends with an error.
Here’s a chunk of log…
This is all based on the example code from here, which is as follows…
Awesome! Looks like it’s working now – I just need to fiddle with the output to extract the actual X, Y and Z readings.
If it does work, I’ll let you all know, but until I’ve proved it, I’ll leave this ticket open.
Okay, it looks like it’s now working, but there’s enough error in the sensor, and I’ve yet to prove that I’ve really resolved this. Here’s the code snippet:
If anyone is interested in this, I invite you to download the source and try it out – I’m yet to prove scientifically that this is actually correct, even though first glance it looks ok.
Okay, I’ve done some testing – it looks good. I’ve converted the values to G’s, as per the instructions that came with the device – stating that 1 G ~ 200 units (I wish they did a little better than ~200, some indication of the error would have been nice).
If you position the device as per the vendor page, you can see each access hit an acceleration reading of ~ 1.02f.
I think I can close this off now and work on cleaning up the framework.
The code can be checked out at: