Created
February 14, 2013 07:32
-
-
Save natsud1/bcfae5b19e6272912b3e to your computer and use it in GitHub Desktop.
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 <SoftwareSerial.h> | |
#define BTtxPin 2 | |
#define BTrxPin 3 | |
SoftwareSerial BTSerial = SoftwareSerial(BTrxPin, BTtxPin);// set up a new serial port | |
void setup(){ | |
pinMode ( BTrxPin, INPUT ); | |
pinMode ( BTtxPin, OUTPUT ); | |
Serial.begin(9600); | |
BTSerial.begin(9600); | |
} | |
void loop(){ | |
readSPP(); | |
} | |
void readSPP(){ | |
#define DEBUG // Uncomment to print data for debugging | |
char input[30]; | |
uint8_t i = 0; | |
while (BTSerial.available()) { | |
uint8_t incoming = BTSerial.read(); | |
input[i] = incoming; | |
if (input[i] == ';'){ // Keep reading until it reads a semicolon | |
break; | |
} | |
i++; | |
} | |
if(input[0] == 'J') { // Joystick | |
strtok(input, ","); // Ignore 'J' | |
float sppData1 = atof(strtok(NULL, ",")); // x-axis | |
float sppData2 = atof(strtok(NULL, ";")); // y-axis | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" X-axis "); | |
Serial.print(sppData1); | |
Serial.print(" Y-axis "); | |
Serial.println(sppData2); | |
#endif | |
} | |
else if(input[0] == 'M') { // IMU | |
strtok(input, ","); // Ignore 'I' | |
double sppData1 = atof(strtok(NULL, ",")); // Pitch | |
double sppData2 = atof(strtok(NULL, ";")); // Roll | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" Pitch "); | |
Serial.print(sppData1); | |
Serial.print(" Roll "); | |
Serial.println(sppData2); | |
#endif | |
} | |
else if(input[0] == 'S'){ // Stop | |
int steer = 512; | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" Steer = "); | |
Serial.println(steer); | |
#endif | |
} | |
else if(input[0] == 'P') { | |
strtok(input, ","); // Ignore 'P' | |
double Kp = atof(strtok(NULL, ";")); | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" P = "); | |
Serial.println(Kp); | |
#endif | |
} | |
else if(input[0] == 'I') { | |
strtok(input, ","); // Ignore 'I' | |
double Ki = atof(strtok(NULL, ";")); | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" I = "); | |
Serial.println(Ki); | |
#endif | |
} | |
else if(input[0] == 'D') { | |
strtok(input, ","); // Ignore 'D' | |
double Kd = atof(strtok(NULL, ";")); | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" D = "); | |
Serial.println(Kd); | |
#endif | |
} | |
else if(input[0] == 'T') { // Target Angle | |
strtok(input, ","); // Ignore 'T' | |
double targetAngle = atof(strtok(NULL, ";")); | |
#ifdef DEBUG | |
Serial.print("Flag "); | |
Serial.print(input[0]); | |
Serial.print(" T = "); | |
Serial.println(targetAngle); | |
#endif | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment