This shows you the differences between two versions of the page.
|
digispark:tutorials:9dof [2015/01/12 17:56] digistump [Programming:] |
digispark:tutorials:9dof [2016/06/09 12:03] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======Pro 9DOF Shield Kit Tutorial====== | ||
| - | =====Product Description:===== | ||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof1.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof1.JPG}}]] | ||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof4.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof4.JPG}}]] | ||
| - | |||
| - | This is an unassembled kit and requires basic soldering. This is designed for use with the Digispark development board, which is not included. | ||
| - | |||
| - | =====Parts:===== | ||
| - | |||
| - | ^ Part ^ Quantity ^Identification^ | ||
| - | | Pro Combo PCB|1| | | ||
| - | | 4.7K (4K7) Ohm 1/4W resistor 1% or 5%|2| Yellow - Violet - Red| | ||
| - | | I2C 9DOF Module |1|| | ||
| - | | 1x40pin male 0.1" pitch header| 29 pins worth| | | ||
| - | |||
| - | |||
| - | **Resistor Values:** For more information on how to identify the value of the resistors we recommend these sites: | ||
| - | A nice simple resistor calculator: http://www.ealnet.com/m-eal/resistor/resistor.htm | ||
| - | A comprehensive article on identification: http://www.diyaudioandvideo.com/Electronics/Color/ | ||
| - | |||
| - | **Soldering:** If you are new to soldering we recommend the following tutorials: | ||
| - | Soldering Basics (http://www.sparkfun.com/tutorials/106) and Soldering Crash Course from the folks at Sparkfun (http://www.sparkfun.com/tutorials/354). | ||
| - | How to solder from the Curious Inventor: http://store.curiousinventor.com/guides/How_to_Solder | ||
| - | |||
| - | Adafruit has this excellent guide that starts with the tools needed and then shows detailed pictures, including some of the common problems that beginners experience (http://learn.adafruit.com/adafruit-guide-excellent-soldering) | ||
| - | |||
| - | **We assume for these assembly instructions that you know the basics of thru-hole soldering. If you don't check out the links above, these boards are very easy to solder - we promise!** | ||
| - | |||
| - | |||
| - | =====Assembly:===== | ||
| - | |||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof1.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof1.JPG}}]] | ||
| - | |||
| - | Solder resistors one at a time. Solder the leads and clip off the excess. Repeat for both resistors. | ||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/comboi2c1.JPG|{{https://s3.amazonaws.com/digispark/images/t/comboi2c1.JPG}}]] | ||
| - | |||
| - | |||
| - | Cut two lengths of male headers each 9 pins long and one 3 pins long. Insert into corresponding positions (on the bottom of the board) and solder each pin. | ||
| - | |||
| - | **Tip:** Inserting the headers into a breadboard and then placing the board on top can make this process easier. | ||
| - | |||
| - | **Note:** If you are using stackable headers, use them here instead of the standard male headers. | ||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/comboi2c2.JPG|{{https://s3.amazonaws.com/digispark/images/t/comboi2c2.JPG}}]] | ||
| - | |||
| - | Insert a strip of 8 straight header pins into the front of the board matching the pins labeled for the 9DOF module. Solder the pins on the back. | ||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof2.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof2.JPG}}]] | ||
| - | |||
| - | |||
| - | Insert the 9DOF module onto the front of the board, on to the strip of 8 pins. Solder the pins where they come through the module, optionally snip off the excess. | ||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof3.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof3.JPG}}]] | ||
| - | |||
| - | [[https://s3.amazonaws.com/digispark/images/m/9dof4.JPG|{{https://s3.amazonaws.com/digispark/images/t/9dof4.JPG}}]] | ||
| - | |||
| - | =====Programming:===== | ||
| - | |||
| - | Example can be found in File->Examples->Digispark_Examples->_9DOF_Shield | ||
| - | [code] | ||
| - | #include <Wire.h> | ||
| - | #include <DigiKeyboard.h> | ||
| - | #define ADXL345 (0x53) //address of Accelerometer | ||
| - | #define ADXL345_X (0x32) //register for X value from Accelerometer | ||
| - | #define ADXL345_Y (0x34) //register for Y value from Accelerometer | ||
| - | #define ADXL345_Z (0x36) //register for Z value from Accelerometer | ||
| - | #define HMC5883 (0x1E) //address of Magnetometer | ||
| - | #define ITG3200 (0x68) //address of Gyro | ||
| - | int16_t magX = 0; //X value from Magnetometer | ||
| - | int16_t magY = 0; //Y value from Magnetometer | ||
| - | int16_t magZ = 0; //Z value from Magnetometer | ||
| - | int gyroHX = 0; //HX value from Gyro | ||
| - | int gyroHY = 0; //HY value from Gyro | ||
| - | int gyroHZ = 0; //HZ value from Gyro4097 | ||
| - | void setup() | ||
| - | { | ||
| - | Wire.begin(); | ||
| - | | ||
| - | initAccelerometer(); | ||
| - | | ||
| - | initMagnetometer(); | ||
| - | | ||
| - | initGyro(); | ||
| - | | ||
| - | } | ||
| - | |||
| - | |||
| - | void loop() | ||
| - | { | ||
| - | | ||
| - | DigiKeyboard.println(readAccelerometer(ADXL345_X)); | ||
| - | DigiKeyboard.println(readAccelerometer(ADXL345_Y)); | ||
| - | DigiKeyboard.println(readAccelerometer(ADXL345_Z)); | ||
| - | DigiKeyboard.delay(1000); | ||
| - | readMagnetometer(); | ||
| - | DigiKeyboard.println(magX); | ||
| - | DigiKeyboard.println(magY); | ||
| - | DigiKeyboard.println(magZ); | ||
| - | DigiKeyboard.println(getHeading()); | ||
| - | DigiKeyboard.delay(1000); | ||
| - | readGyro(); | ||
| - | DigiKeyboard.println(gyroHX); | ||
| - | DigiKeyboard.println(gyroHY); | ||
| - | DigiKeyboard.println(gyroHZ); | ||
| - | DigiKeyboard.delay(1000); | ||
| - | | ||
| - | | ||
| - | |||
| - | } | ||
| - | |||
| - | |||
| - | |||
| - | void initGyro(){ | ||
| - | writeRegister(ITG3200, 0x3E, 0x00); //enable | ||
| - | writeRegister(ITG3200, 0x15, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF | ||
| - | writeRegister(ITG3200, 0x16, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19 | ||
| - | writeRegister(ITG3200, 0x17, 0x00); | ||
| - | } | ||
| - | |||
| - | void readGyro(){ | ||
| - | Wire.beginTransmission(ITG3200); | ||
| - | Wire.write(0x1B); //format x y z temp | ||
| - | Wire.endTransmission(); | ||
| - | Wire.requestFrom(ITG3200, (byte)8); | ||
| - | | ||
| - | // Wait around until enough data is available | ||
| - | while (Wire.available() < 8); | ||
| - | uint8_t lo = Wire.read(); | ||
| - | uint8_t hi = Wire.read(); | ||
| - | //throw out first two - don't seem accurate | ||
| - | //gyroTemp = (lo << 8) | hi; // temperature | ||
| - | //gyroTemp = ((double) (gyroTemp + 13200)) / 280; | ||
| - | lo = Wire.read(); | ||
| - | hi = Wire.read(); | ||
| - | gyroHX = (((lo << 8) | hi) + 120 )/ 14.375; | ||
| - | lo = Wire.read(); | ||
| - | hi = Wire.read(); | ||
| - | gyroHY = (((lo << 8) | hi) + 20 )/ 14.375; | ||
| - | lo = Wire.read(); | ||
| - | hi = Wire.read(); | ||
| - | gyroHZ = (((lo << 8) | hi) + 93 )/ 14.375; | ||
| - | |||
| - | } | ||
| - | |||
| - | void initMagnetometer(){ | ||
| - | writeRegister(HMC5883, 0x02, 0x00); //enable | ||
| - | } | ||
| - | |||
| - | |||
| - | void readMagnetometer(){ | ||
| - | Wire.beginTransmission(HMC5883); | ||
| - | Wire.write(0x03); //format X_H_M | ||
| - | Wire.endTransmission(); | ||
| - | Wire.requestFrom(HMC5883, (byte)6); | ||
| - | | ||
| - | // Wait around until enough data is available | ||
| - | while (Wire.available() < 6); | ||
| - | |||
| - | // Note high before low (different than accel) | ||
| - | uint8_t hi = Wire.read(); | ||
| - | uint8_t lo = Wire.read(); | ||
| - | // Shift values to create properly formed integer (low byte first) | ||
| - | magX = (int16_t)(hi | ((int16_t)lo << 8)); | ||
| - | // Note high before low (different than accel) | ||
| - | hi = Wire.read(); | ||
| - | lo = Wire.read(); | ||
| - | // Shift values to create properly formed integer (low byte first) | ||
| - | magY = (int16_t)(hi | ((int16_t)lo << 8)); | ||
| - | // Note high before low (different than accel) | ||
| - | hi = Wire.read(); | ||
| - | lo = Wire.read(); | ||
| - | // Shift values to create properly formed integer (low byte first) | ||
| - | magZ = (int16_t)(hi | ((int16_t)lo << 8)); | ||
| - | } | ||
| - | |||
| - | void initAccelerometer(){ | ||
| - | //if(readRegister(ADXL345,0x00) != 0xE5) | ||
| - | writeRegister(ADXL345, 0x2D, 0x08); //power up and enable measurements | ||
| - | writeRegister(ADXL345, 0x31, 0x01);//set range to +/-4G | ||
| - | } | ||
| - | |||
| - | uint16_t readAccelerometer(uint8_t reg){ | ||
| - | Wire.beginTransmission(ADXL345); | ||
| - | Wire.write(reg); | ||
| - | Wire.endTransmission(); | ||
| - | Wire.requestFrom(ADXL345, 2); | ||
| - | return (uint16_t)(Wire.read() | (Wire.read() << 8)); | ||
| - | } | ||
| - | |||
| - | uint8_t readRegister(uint8_t address, uint8_t reg){ | ||
| - | Wire.beginTransmission(address); | ||
| - | Wire.write(reg); | ||
| - | Wire.endTransmission(); | ||
| - | Wire.requestFrom(address, 1); | ||
| - | return Wire.read(); | ||
| - | } | ||
| - | void writeRegister(uint8_t address, uint8_t reg, uint8_t val){ | ||
| - | Wire.beginTransmission(address); | ||
| - | Wire.write(reg); | ||
| - | Wire.write(val); | ||
| - | Wire.endTransmission(); | ||
| - | } | ||
| - | |||
| - | float getHeading(){ | ||
| - | // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y | ||
| - | // Calculate heading when the magnetometer is level, then correct for signs of axis. | ||
| - | float heading = atan2(magY, magX); | ||
| - | // Correct for when signs are reversed. | ||
| - | if(heading < 0) | ||
| - | heading += 2*PI; | ||
| - | // Check for wrap due to addition of declination. | ||
| - | if(heading > 2*PI) | ||
| - | heading -= 2*PI; | ||
| - | // Convert radians to degrees for readability. | ||
| - | heading = heading * 180/M_PI; | ||
| - | return heading; | ||
| - | } | ||
| - | [/code] | ||