Hi Community,
I am transferring data from a Jetson Nano via pyserial to arduino. I can transfer the data no problem but having trouble parsing the data for motor control.
The data seems to be in a Char format but need it to be a string. I am sure this is a simple issue but it just cant seem to crack it. The data structure looks like this ('a',200). I can split the char and the integer and print them out in the serial monitor but cant seem to transfer the data to match the drive command. It has to do with the data type.
I would appreciate your assistance.
I want the data in the code below from 'messageFromPC' , which is a char like 'a' for example to connect in with the "if (Direction =="a"). The full version of the code is below this section.
void showParsedData() {
// Serial.print("Message ");
// Serial.println(messageFromPC);
// Serial.print("Integer ");
// Serial.println(integerFromPC);
}
String Direction = messageFromPC; {
Serial.print(Direction);
if (Direction == "a") {
ADVANCE();
}
else if (Direction == "z") {
STOP();
}
else if (Direction == "b") {
RIGHT_1();
}
else if (Direction == "c") {
RIGHT_2();
}
This is the full code below:
//Motor Pins
#define PWMA 12 //Motor A Speed
#define DIRA1 34
#define DIRA2 35 //Motor A Direction
#define PWMB 8 //Motor B Speed
#define DIRB1 37
#define DIRB2 36 //Motor B Direction
#define PWMC 9 //Motor C Speed
#define DIRC1 43
#define DIRC2 42 //Motor C Direction
#define PWMD 5 //Motor D Speed
#define DIRD1 26
#define DIRD2 27 //Motor A Direction
int error = 0;
byte type = 0;
byte vibrate = 0;
String incomingByte;
String Direction;
int pwmint = 0;
// variables to hold the parsed data
const byte numChars = 11;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
boolean newData = false;
// Sets the character types for markers
void (* resetFunc) (void) = 0;
//Motor A
#define MOTORA_FORWARD(pwm) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,HIGH);analogWrite(PWMA,pwm);}while(0)
#define MOTORA_STOP(x) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,LOW); analogWrite(PWMA,0);}while(0)
#define MOTORA_BACKOFF(pwm) do{digitalWrite(DIRA1,HIGH);digitalWrite(DIRA2,LOW); analogWrite(PWMA,pwm);}while(0)
//Motor B
#define MOTORB_FORWARD(pwm) do{digitalWrite(DIRB1,HIGH); digitalWrite(DIRB2,LOW);analogWrite(PWMB,pwm);}while(0)
#define MOTORB_STOP(x) do{digitalWrite(DIRB1,LOW); digitalWrite(DIRB2,LOW); analogWrite(PWMB,0);}while(0)
#define MOTORB_BACKOFF(pwm) do{digitalWrite(DIRB1,LOW);digitalWrite(DIRB2,HIGH); analogWrite(PWMB,pwm);}while(0)
#define MAX_PWM 200
#define MIN_PWM 130
int Motor_PWM = 130;
//Controlling Motor Motion Macro Definition
// ↑A-----B↑
// | ↑ |
// | | |
// ↑C-----D↑
void ADVANCE()
{
MOTORA_FORWARD(Motor_PWM); MOTORB_FORWARD(Motor_PWM);
MOTORC_FORWARD(Motor_PWM); MOTORD_FORWARD(Motor_PWM);
}
// ↓A-----B↓
// | | |
// | ↓ |
// ↓C-----D↓
void BACK()
{
MOTORA_BACKOFF(Motor_PWM); MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM); MOTORD_BACKOFF(Motor_PWM);
}
// ↑A-----B=
// | ↖ |
// | ↖ |
// =C-----D↑
void LEFT_1()
{
MOTORA_FORWARD(Motor_PWM); MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM); MOTORD_FORWARD(Motor_PWM);
}
// ↓A-----B↑
// | ← |
// | ← |
// ↑C-----D↓
void LEFT_2()
{
MOTORA_BACKOFF(Motor_PWM); MOTORB_FORWARD(Motor_PWM);
MOTORC_FORWARD(Motor_PWM); MOTORD_BACKOFF(Motor_PWM);
}
// ↓A-----B=
// | ↙ |
// | ↙ |
// =C-----D↓
void LEFT_3()
{
MOTORA_BACKOFF(Motor_PWM); MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM); MOTORD_BACKOFF(Motor_PWM);
}
// ↓A-----B↑
// | ← |
// | ↓ ↑ |
// | → |
// ↓C-----D↑
void LEFT_4()
{
MOTORA_BACKOFF(Motor_PWM); MOTORB_FORWARD(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM); MOTORD_FORWARD(Motor_PWM);
}
// =A-----B↑
// | ↗ |
// | ↗ |
// ↑C-----D=
void RIGHT_1()
{
MOTORA_STOP(Motor_PWM); MOTORB_FORWARD(Motor_PWM);
MOTORC_FORWARD(Motor_PWM); MOTORD_STOP(Motor_PWM);
}
// ↑A-----B↓
// | → |
// | → |
// ↓C-----D↑
void RIGHT_2()
{
MOTORA_FORWARD(Motor_PWM); MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM); MOTORD_FORWARD(Motor_PWM);
}
// =A-----B↓
// | ↘ |
// | ↘ |
// ↓C-----D=
void RIGHT_3()
{
MOTORA_STOP(Motor_PWM); MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM); MOTORD_STOP(Motor_PWM);
}
// ↑A-----B↓
// | → |
// | ↑ ↓ |
// | ← |
// ↑C-----D↓
void RIGHT_4()
{
MOTORA_FORWARD(Motor_PWM); MOTORB_BACKOFF(Motor_PWM);
MOTORC_FORWARD(Motor_PWM); MOTORD_BACKOFF(Motor_PWM);
}
// =A-----B=
// | = |
// | = |
// =C-----D=
void STOP()
{
MOTORA_STOP(Motor_PWM); MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM); MOTORD_STOP(Motor_PWM);
}
void IO_init()
{
pinMode(PWMA, OUTPUT);
pinMode(DIRA1, OUTPUT);
pinMode(DIRA2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRB1, OUTPUT);
pinMode(DIRB2, OUTPUT);
STOP();
}
//===========================
void setup()
{
Serial.begin(115200);
Serial.println("<Arduino is ready>");
IO_init();
}
//==========================
// Receiving data from Python
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
}
//============
void showParsedData() {
// Serial.print("Message ");
// Serial.println(messageFromPC);
// Serial.print("Integer ");
// Serial.println(integerFromPC);
}
String Direction = messageFromPC; {
Serial.print(Direction);
if (Direction == "a") {
ADVANCE();
}
else if (Direction == "z") {
STOP();
}
else if (Direction == "b") {
RIGHT_1();
}
else if (Direction == "c") {
RIGHT_2();
}
else if (Direction == "d") {
RIGHT_3();
}
else if (Direction == "e") {
BACK();
}
else if (Direction == "f") {
LEFT_3();
}
else if (Direction == "g") {
LEFT_2();
}
else if (Direction == "h") {
LEFT_1();
}
else if (Direction == "i") {
RIGHT_4();
}
else if (Direction == "j") {
LEFT_4();
}
else {
Serial.write("invalid");
}
}
//============================
I really appreciate your assistance.