Difference between revisions of "Project details"
Jwmaseberg (talk | contribs) |
|||
| Line 1: | Line 1: | ||
Authors: Soobum Kim and Jack Maseberg | Authors: Soobum Kim and Jack Maseberg | ||
| + | |||
| + | |||
| + | Here is my recent arduino code for our robot. | ||
| + | |||
| + | /* | ||
| + | prototype_unified_test | ||
| + | */ | ||
| + | |||
| + | #include <Wire.h> | ||
| + | |||
| + | |||
| + | char val; // variable to receive data from the serial port | ||
| + | int ledpin = 13; // LED connected to pin 13 (on-board LED) | ||
| + | int HMC6352SlaveAddress = 0x42; | ||
| + | int HMC6352ReadAddress = 0x41; //"A" in hex, A command is: | ||
| + | int headingValue; | ||
| + | const int LMDir = 12; // Left motor direction | ||
| + | const int RMDir = 13; // Right motor direction | ||
| + | const int LMSpeed = 9; // PWM for Left motor connected to digital pin 9 | ||
| + | const int RMSpeed = 10; // PWM for Right motor connected to digital pin 10 | ||
| + | |||
| + | |||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(2, OUTPUT); | ||
| + | pinMode(ledpin = 13, OUTPUT); // pin 13 (on-board LED) as OUTPUT | ||
| + | TCCR1B = TCCR1B & 0b11111000 | 0x01; //set PWM freq to 31250 Hz on pins 9 and 10 | ||
| + | pinMode(12, OUTPUT); // sets the pin as output | ||
| + | pinMode(13, OUTPUT); // sets the pin as output | ||
| + | pinMode(LMSpeed, OUTPUT); // sets the pin as output | ||
| + | pinMode(RMSpeed, OUTPUT); // sets the pin as output | ||
| + | // "The Wire library uses 7 bit addresses throughout. | ||
| + | //If you have a datasheet or sample code that uses 8 bit address, | ||
| + | //you'll want to drop the low bit (i.e. shift the value one bit to the right), | ||
| + | //yielding an address between 0 and 127." | ||
| + | HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required | ||
| + | Wire.begin(); | ||
| + | |||
| + | |||
| + | Serial.begin(115200); // start serial communication at 115200bps | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | digitalWrite(2, LOW); | ||
| + | if( Serial.available() ) // if data is available to read | ||
| + | {;} | ||
| + | val = Serial.read(); // read it and store it in 'val' | ||
| + | |||
| + | if( val == '0' ) // if '0' was received led 13 is switched off | ||
| + | |||
| + | { | ||
| + | digitalWrite(ledpin, LOW); // turn Off pin 13 off | ||
| + | delay(200); // waits for a second | ||
| + | Serial.println("13 off"); | ||
| + | } | ||
| + | |||
| + | if( val == '1' ) // if '1' was received led 13 on | ||
| + | |||
| + | { | ||
| + | Serial.println("13 on"); | ||
| + | digitalWrite(13, HIGH); // set the LED on | ||
| + | delay(200); // wait for a second | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | if(val == 'f' ) | ||
| + | |||
| + | { | ||
| + | Serial.println("Motor Forward"); | ||
| + | digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | delay(10); | ||
| + | } | ||
| + | |||
| + | if(val == 'b' ) | ||
| + | { | ||
| + | Serial.println("Motor Backward"); | ||
| + | digitalWrite(LMDir, HIGH); // digital write to set direction (LOW or HIGH) | ||
| + | digitalWrite(RMDir, HIGH); // digital write to set direction (LOW or HIGH) | ||
| + | analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | delay(10); | ||
| + | } | ||
| + | |||
| + | if(val == 's' ) | ||
| + | { | ||
| + | Serial.println("Motor Stop"); | ||
| + | digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | analogWrite(LMSpeed, 0); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | analogWrite(RMSpeed, 0); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | delay(10); | ||
| + | } | ||
| + | |||
| + | if(val == 'r') | ||
| + | { | ||
| + | Serial.println("Motor Rotation Right"); | ||
| + | digitalWrite(LMDir, HIGH); // digital write to set direction (LOW or HIGH) | ||
| + | digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | delay(10); | ||
| + | } | ||
| + | |||
| + | if(val == 'l') | ||
| + | { | ||
| + | Serial.println("Motor Rotation Left"); | ||
| + | digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH) | ||
| + | digitalWrite(RMDir, HIGH); // digital write to set direction (LOW or HIGH) | ||
| + | analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle | ||
| + | delay(10); | ||
| + | } | ||
| + | |||
| + | |||
| + | //"Get Data. Compensate and Calculate New Heading" | ||
| + | Wire.beginTransmission(HMC6352SlaveAddress); | ||
| + | Wire.write(HMC6352ReadAddress); // The "Get Data" command | ||
| + | Wire.endTransmission(); | ||
| + | |||
| + | //time delays required by HMC6352 upon receipt of the command | ||
| + | //Get Data. Compensate and Calculate New Heading : 6ms | ||
| + | delay(6); | ||
| + | |||
| + | Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB | ||
| + | |||
| + | //"The heading output data will be the value in tenths of degrees | ||
| + | //from zero to 3599 and provided in binary format over the two bytes." | ||
| + | byte MSB = Wire.read(); | ||
| + | byte LSB = Wire.read(); | ||
| + | |||
| + | float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum) | ||
| + | float headingInt = headingSum / 10; | ||
| + | |||
| + | Serial.print(headingInt); | ||
| + | Serial.println(" degrees"); | ||
| + | |||
| + | delay(100); | ||
| + | |||
| + | |||
| + | } | ||
Revision as of 00:58, 22 January 2012
Authors: Soobum Kim and Jack Maseberg
Here is my recent arduino code for our robot.
/* prototype_unified_test
- /
- include <Wire.h>
char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 13 (on-board LED)
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41; //"A" in hex, A command is:
int headingValue;
const int LMDir = 12; // Left motor direction
const int RMDir = 13; // Right motor direction
const int LMSpeed = 9; // PWM for Left motor connected to digital pin 9
const int RMSpeed = 10; // PWM for Right motor connected to digital pin 10
void setup() {
pinMode(2, OUTPUT); pinMode(ledpin = 13, OUTPUT); // pin 13 (on-board LED) as OUTPUT TCCR1B = TCCR1B & 0b11111000 | 0x01; //set PWM freq to 31250 Hz on pins 9 and 10 pinMode(12, OUTPUT); // sets the pin as output pinMode(13, OUTPUT); // sets the pin as output pinMode(LMSpeed, OUTPUT); // sets the pin as output pinMode(RMSpeed, OUTPUT); // sets the pin as output // "The Wire library uses 7 bit addresses throughout. //If you have a datasheet or sample code that uses 8 bit address, //you'll want to drop the low bit (i.e. shift the value one bit to the right), //yielding an address between 0 and 127." HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required Wire.begin();
Serial.begin(115200); // start serial communication at 115200bps
}
void loop() {
digitalWrite(2, LOW);
if( Serial.available() ) // if data is available to read
{;}
val = Serial.read(); // read it and store it in 'val'
if( val == '0' ) // if '0' was received led 13 is switched off
{
digitalWrite(ledpin, LOW); // turn Off pin 13 off
delay(200); // waits for a second Serial.println("13 off");
}
if( val == '1' ) // if '1' was received led 13 on
{
Serial.println("13 on");
digitalWrite(13, HIGH); // set the LED on
delay(200); // wait for a second
}
if(val == 'f' )
{
Serial.println("Motor Forward");
digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH)
digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH)
analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
delay(10);
}
if(val == 'b' ) {
Serial.println("Motor Backward");
digitalWrite(LMDir, HIGH); // digital write to set direction (LOW or HIGH)
digitalWrite(RMDir, HIGH); // digital write to set direction (LOW or HIGH)
analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
delay(10);
}
if(val == 's' ) {
Serial.println("Motor Stop");
digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH)
digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH)
analogWrite(LMSpeed, 0); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
analogWrite(RMSpeed, 0); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
delay(10);
}
if(val == 'r') {
Serial.println("Motor Rotation Right");
digitalWrite(LMDir, HIGH); // digital write to set direction (LOW or HIGH)
digitalWrite(RMDir, LOW); // digital write to set direction (LOW or HIGH)
analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
delay(10);
}
if(val == 'l') {
Serial.println("Motor Rotation Left");
digitalWrite(LMDir, LOW); // digital write to set direction (LOW or HIGH)
digitalWrite(RMDir, HIGH); // digital write to set direction (LOW or HIGH)
analogWrite(LMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
analogWrite(RMSpeed, 200); // analogWrite values from 0 to 255 correspond to 0% and 100% duty cycle
delay(10);
}
//"Get Data. Compensate and Calculate New Heading" Wire.beginTransmission(HMC6352SlaveAddress); Wire.write(HMC6352ReadAddress); // The "Get Data" command Wire.endTransmission();
//time delays required by HMC6352 upon receipt of the command //Get Data. Compensate and Calculate New Heading : 6ms delay(6);
Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB
//"The heading output data will be the value in tenths of degrees //from zero to 3599 and provided in binary format over the two bytes." byte MSB = Wire.read(); byte LSB = Wire.read();
float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum) float headingInt = headingSum / 10;
Serial.print(headingInt);
Serial.println(" degrees");
delay(100);
}