austinbv / dino

Dino is a ruby gem that helps you bootstrap prototyping with an Arduino
MIT License
389 stars 84 forks source link

How to add support for the Pololu AltIMU-10 v4 Gyro, Accelerometer, Compass, and Altimeter? #75

Open hebertodelrio opened 10 years ago

hebertodelrio commented 10 years ago

I am new to the arduino platform, but I am interested in using a Pololu AltIMU-10 v3 in a project, are there any guidelines how to add a component to the Dino gem in order to use the AltIMU-10 from ruby?

Thanks in advance

P.S. any suggestions are welcome

vickash commented 10 years ago

Just looked up this component. It uses an I2C interface, so what you really need is for dino to be able to communicate via an I2C bus. In a "pure" Arduino sketch, you use the Wire library for that. See here: http://arduino.cc/en/reference/wire

If you have one of these parts already, I suggest you play around with it using any "pure" Arduino examples you can find first. This will let you get a feel for it, and the Wire library.

Once you understand that, you can follow the same pattern as @supherman did for LCD support. Code is in #32. Explanation is in the first few messages of #34. The basic idea is to leverage the existing Arduino Wire library by including it in the dino sketch, and then talking to it from Ruby.

As of the 0.12.0 branch, the Dino::Message class in Ruby, and the dino sketch itself, take care all the message encoding and decoding for you, so you just need to do a couple things on the Arduino side to hook up your library.

Using the LCD as a template:

This line in the Arduino code lets the command '10' invoke the LCD library: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L86 You can use 14 or 15. If there's a conflict with another branch later, easy to fix.

This function passes execution to DinoLCD.cpp, giving it the variables val and auxMsg: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L265-L272 You can pretty much copy and edit this.

At this point, I should clarify the message format. It consists of 4 parts, I'll refer to them by the Arduino variable names:

cmd - Numeric key for a command to execute. Eg. 1 = digitalWrite. In your case, this signifies your entire library, and not a specific command. This is the 14 or 15 mentioned earlier.

pin - Numeric key for the pin to execute the command on. Not necessary for I2C.

val - Numeric data for the command. For example, if the command was 1 (digital write), this could be 1 for on, or 0 for off. In your case, as with the LCD example, you'll use this to signify the "sub-command" inside your library. Repurposing it this way is a good idea since you'll want more data than a simple 4 digit number, but the commands in your library will be numbered.

auxMsg - This is a text string up to 255 characters. This is where you want to store the data you want to send to the Wire library.

Wrt handling messages within your library, follow the pattern used for the LCD library here: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/DinoLCD.cpp#L21-L43

Context has changed, so the variable cmd in there, is actually val we passed in, and message is auxMsg from the main library. Remember this line: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L271

Once you get the Arduino side of the code done, you can do a simple Ruby class that uses Dino::Message directly to talk to it, similar to the LCD class: https://github.com/austinbv/dino/blob/0.12.0-wip/lib/dino/components/lcd.rb

Give it a try and if you get stuck start a pull request and I'll help.

hebertodelrio commented 10 years ago

Thanks a lot

HDR

On Aug 18, 2014, at 2:15 AM, Vickash notifications@github.com wrote:

Just looked up this component. It uses an I2C interface, so what you really need is for dino to be able to communicate via an I2C bus. In a "pure" Arduino sketch, you use the Wire library for that. See here: http://arduino.cc/en/reference/wire

If you have one of these parts already, I suggest you play around with it using any "pure" Arduino examples you can find first. This will let you get a feel for it, and the Wire library.

Once you understand that, you can follow the same pattern as @supherman did for LCD support. Code is in #32. Explanation is in the first few messages of #34. The basic idea is to leverage the existing Arduino Wire library by including it in the dino sketch, and then talking to it from Ruby.

As of the 0.12.0 branch, the Dino::Message class in Ruby, and the dino sketch itself, take care all the message encoding and decoding for you, so you just need to do a couple things on the Arduino side to hook up your library.

Using the LCD as a template:

This line in the Arduino code lets the command '10' invoke the LCD library: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L86 You can use 14 or 15. If there's a conflict with another branch later, easy to fix.

This function passes execution to DinoLCD.cpp, giving it the variables val and auxMsg: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L265-L272 You can pretty much copy and edit this.

At this point, I should clarify the message format. It consists of 3 parts, I'll refer to them by the Arduino variable names:

cmd - Numeric key for a command to execute. Eg. 1 = digitalWrite. In your case, this signifies your entire library, and not a specific command. This is the 14 or 15 mentioned earlier.

pin - Numeric key for the pin to execute the command on. Not necessary for I2C.

val - Numeric data for the command. For example, if the command was 1 (digital write), this could be 1 for on, or 0 for off. In your case, as with the LCD example, you'll use this to signify the "sub-command" inside your library. Repurposing it this way is a good idea since you'll want more data than a simple 4 digit number, but the commands in your library will be numbered.

auxMsg - This is a text string up to 255 characters. This is where you want to store the data you want to send to the Wire library.

Wrt handling messages within your library, follow the pattern used for the LCD library here: Just like: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/DinoLCD.cpp#L21-L43

Context has changed, so the variable cmd in there, is actually val we passed in, and message is auxMsg from the main library. Remember this line: https://github.com/austinbv/dino/blob/0.12.0-wip/src/lib/Dino.cpp#L271

Once you get the Arduino side of the code done, you can do a simple Ruby class that uses Dino::Message directly to talk to it, similar to the LCD class: https://github.com/austinbv/dino/blob/0.12.0-wip/lib/dino/components/lcd.rb

Give it a try and if you get stuck start a pull request and I'll help.

— Reply to this email directly or view it on GitHub.