Created
January 2, 2016 05:51
-
-
Save treeherder/8aa978cc764efa1a084e to your computer and use it in GitHub Desktop.
just a simple sketch to cludge together all of the various components of the hexapod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_BMP085.h> | |
#include "MPU6050.h" | |
#include "HMC5883L.h" | |
#include <Adafruit_PWMServoDriver.h> | |
// called this way, it uses the default address 0x40 | |
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(); | |
// you can also call it with a different address you want | |
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41); | |
// Depending on your servo make, the pulse width min and max may vary, you | |
// want these to be as small/large as possible without hitting the hard stop | |
// for max range. You'll have to tweak them as necessary to match the servos you | |
// have! | |
#define SERVOMIN 300 // this is the 'minimum' pulse length count (out of 4096) | |
#define SERVOMAX 400 // this is the 'maximum' pulse length count (out of 4096) | |
// our servo # counter | |
uint8_t servonum = 0; | |
int r0 = 0; //value of select pin at the 4051 (s0) | |
int r1 = 0; //value of select pin at the 4051 (s1) | |
int r2 = 0; //value of select pin at the 4051 (s2) | |
int register_pin = 0; //which y pin we are selecting | |
MPU6050 accelgyro; | |
int16_t ax, ay, az; | |
int16_t gx, gy, gz; | |
HMC5883L mag; | |
int16_t mx, my, mz; | |
// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated | |
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read, | |
// not so easy to parse, and slow(er) over UART. | |
#define OUTPUT_READABLE_ACCELGYRO | |
// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit | |
// binary, one right after the other. This is very fast (as fast as possible | |
// without compression or data loss), and easy to parse, but impossible to read | |
// for a human. | |
//#define OUTPUT_BINARY_ACCELGYRO | |
#define LED_PIN 13 | |
bool blinkState = false; | |
// MPU6050 HMC5883L BMP085 | |
Adafruit_BMP085 bmp; | |
void setup() { | |
Wire.begin(); | |
Serial.begin(38400); | |
pinMode(2, OUTPUT); // Select 0 | |
pinMode(3, OUTPUT); // S1 | |
pinMode(4, OUTPUT); // S2 | |
pinMode(5, INPUT); // Enable 1 | |
pinMode(6, INPUT); // E2 | |
pinMode(7, INPUT); // E3 | |
digitalWrite(5, HIGH); | |
digitalWrite(6, HIGH); | |
digitalWrite(7, HIGH); | |
Serial.println("16 channel Servo test!"); | |
#ifdef ESP8266 | |
Wire.pins(2, 14); // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14 | |
#endif | |
pwm1.begin(); | |
pwm2.begin(); | |
pwm1.setPWMFreq(60); // Analog servos run at ~60 Hz updates | |
pwm2.setPWMFreq(60); // Analog servos run at ~60 Hz updates | |
yield(); | |
Serial.println("Initializing I2C devices..."); | |
mag.initialize(); | |
// verify connection | |
Serial.println("Testing device connections..."); | |
Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed"); | |
// initialize device | |
Serial.println("Initializing I2C devices..."); | |
accelgyro.initialize(); | |
// verify connection | |
Serial.println("Testing device connections..."); | |
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); | |
if (!bmp.begin()) { | |
Serial.println("Could not find a valid BMP085 sensor, check wiring!"); | |
while (1) {} | |
} | |
pinMode(LED_PIN, OUTPUT); | |
} | |
void loop() { | |
// read raw heading measurements from device | |
mag.getHeading(&mx, &my, &mz); | |
// display tab-separated gyro x/y/z values | |
Serial.print("mag:\t"); | |
Serial.print(mx); Serial.print("\t"); | |
Serial.print(my); Serial.print("\t"); | |
Serial.print(mz); Serial.print("\t"); | |
Serial.println("."); | |
// To calculate heading in degrees. 0 degree indicates North | |
float heading = atan2(my, mx); | |
if(heading < 0) | |
heading += 2 * M_PI; | |
Serial.print("heading:\t"); | |
Serial.println(heading * 180/M_PI); | |
Serial.print("Temperature = "); | |
Serial.print(bmp.readTemperature()); | |
Serial.println(" *C"); | |
Serial.print("Pressure = "); | |
Serial.print(bmp.readPressure()); | |
Serial.println(" Pa"); | |
// Calculate altitude assuming 'standard' barometric | |
// pressure of 1013.25 millibar = 101325 Pascal | |
Serial.print("Altitude = "); | |
Serial.print(bmp.readAltitude()); | |
Serial.println(" meters"); | |
// you can get a more precise measurement of altitude | |
// if you know the current sea level pressure which will | |
// vary with weather and such. If it is 1015 millibars | |
// that is equal to 101500 Pascals. | |
Serial.print("Real altitude = "); | |
Serial.print(bmp.readAltitude(101500)); | |
Serial.println(" meters"); | |
Serial.println(); | |
// read raw accel/gyro measurements from device | |
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); | |
// these methods (and a few others) are also available | |
//accelgyro.getAcceleration(&ax, &ay, &az); | |
//accelgyro.getRotation(&gx, &gy, &gz); | |
#ifdef OUTPUT_READABLE_ACCELGYRO | |
// display tab-separated accel/gyro x/y/z values | |
Serial.print("a/g:\t"); | |
Serial.print(ax); Serial.print("\t"); | |
Serial.print(ay); Serial.print("\t"); | |
Serial.print(az); Serial.print("\t"); | |
Serial.print(gx); Serial.print("\t"); | |
Serial.print(gy); Serial.print("\t"); | |
Serial.println(gz); | |
#endif | |
#ifdef OUTPUT_BINARY_ACCELGYRO | |
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF)); | |
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF)); | |
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF)); | |
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF)); | |
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF)); | |
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF)); | |
#endif | |
// blink LED to indicate activity | |
blinkState = !blinkState; | |
digitalWrite(LED_PIN, blinkState); | |
for (register_pin=0; register_pin<=7; register_pin++) { | |
for (int reg = 0; reg <= 2; reg ++){ | |
// select the bit | |
r0 = bitRead(register_pin,0); // use this with arduino 0013 (and newer versions) | |
r1 = bitRead(register_pin,1); // use this with arduino 0013 (and newer versions) | |
r2 = bitRead(register_pin,2); // use this with arduino 0013 (and newer versions) | |
//r0 = register_pin & 0x01; // old version of setting the bits | |
//r1 = (register_pin>>1) & 0x01; // old version of setting the bits | |
//r2 = (register_pin>>2) & 0x01; // old version of setting the bits | |
digitalWrite(2, r0); | |
digitalWrite(3, r1); | |
digitalWrite(4, r2); | |
// output the pin value over serial | |
Serial.print(" analog "); | |
Serial.print(reg); | |
Serial.print(" pin "); | |
Serial.print(register_pin); | |
Serial.print(" "); | |
Serial.println(analogRead(reg)); | |
} | |
} | |
// Drive each servo one at a time | |
Serial.println("setting"); | |
pwm1.setPWM(0, 0, 250); | |
pwm2.setPWM(0, 0, 400); | |
pwm1.setPWM(3, 0, 350); | |
pwm2.setPWM(3, 0, 400); | |
pwm1.setPWM(6, 0, 400); | |
pwm2.setPWM(6, 0, 350);// NEAR THE PLUG | |
pwm1.setPWM(1, 0, 350); | |
pwm2.setPWM(1, 0, 350); | |
pwm1.setPWM(4, 0, 250); | |
pwm2.setPWM(4, 0, 300); | |
pwm1.setPWM(7, 0, 250); | |
pwm2.setPWM(7, 0, 300); | |
//feet | |
pwm1.setPWM(2, 0, 250); | |
pwm2.setPWM(2, 0, 250); | |
pwm1.setPWM(5, 0, 250); | |
pwm2.setPWM(5, 0, 150); | |
pwm1.setPWM(8, 0, 250); | |
pwm2.setPWM(8, 0, 250); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment