Hi,
I am working on a remote controlled Arduino boat, which will be remotely controlled by LabVIEW and Xbee's.
This boat has two motos with direction and speed control via dual H bridge motor driver.
The serial signal send from Labview is:
[port_direction,port_speed,stbd_direction,stbd_speed;]
There the incoming string is separated with "," and finally knows it is done sending the whole code with ";"
An example serial command would be, 1,08,0,05; which 1 is port motor direction forward, 08 is port motor speed; 0 is starboard motor reverse and 05 is starboard motor speed.
To debug, I am using Arduino IDE serial motinor, typing an example signal of 1,08,0,05; and with Serial.print in fuction definion which sets the motors direction and speed, I am getting feedback.
But this feedback goes back to zero immediately after I send the serial command. Without serial command coming continueslly, these values goes back to zero.
How can I store and reflect the final command I have send to Arduino without sending serial data continuously?
//Define Arduino pins
#define M1_IN1 7 //Stbd Motor FWD, make HIGH to function
#define M1_IN2 3 //Stbd Motor REV, make HIGH to function
#define M2_IN1 8 //Port Motor FWD, make HIGH to function
#define M2_IN2 2 //Port Motor REV, make HIGH to function
#define EN 9 //Sleep mode pin, normally LOW, make HIGH to start motors, LOW to stop all
#define M1_D2 6 //Stbd Motor speed (0-255)
#define M2_D2 5 //Port Motor speed (0-255)
#define M1FB A0 //Stbd Motor current feedback, 525 mV/A, 9mA per count (5V = 1024 ADC)
#define M2FB A1 //Port Motor current feedback, 525 mV/A, 9mA per count (5V = 1024 ADC)
char labViewData[100]={}; //Create a char variable to store incoming data string from LabVIEW
int labViewDataLength=0,commaCount=0;//Initialization to separate incoming strings
String port_direction="",port_speed="",stbd_direction="",stbd_speed=""; //Saves decoded string
static int portDirection,portSpeed,stbdDirection,stbdSpeed; //Stores decoded data as integer
int portCurrent=0, stbdCurrent=0; //Stores motor current feedback
void setup(){
pinMode(M1_IN1, OUTPUT); //Stbd Motor FWD
pinMode(M1_IN2, OUTPUT); //Stbd Motor REV
pinMode(M2_IN1, OUTPUT); //Port Motor FWD
pinMode(M2_IN2, OUTPUT); //Stbd Motor REV
pinMode(M1_D2, OUTPUT); //Stbd Motor Speed
pinMode(M2_D2, OUTPUT); //Port Motor Speed
pinMode(EN, OUTPUT); //Sleep mode pin
pinMode(M1FB,INPUT); //Stbd motor current feedback
pinMode(M2FB,INPUT); //Port motor current feedback
Serial.begin(9600); //Initialize serial communication and set baud rate
}
void loop(){
if(Serial.available()){
// Here we read the data from LabVIEW, seperate it after every "," and finish reading after ";"
labViewDataLength=Serial.readBytesUntil(';',labViewData,50); // read the command LabVIEW send
for(int i=0;i<labViewDataLength;i++){
// separate data
if(labViewData[i]==','){
// increase comma count
commaCount++;
}
if(commaCount==0 && labViewData[i]!=','){
// read port motor direction
port_direction=port_direction+labViewData[i];
}else if(commaCount==1 && labViewData[i]!=','){
// read port motor speed
port_speed=port_speed+labViewData[i];
}else if(commaCount==2 && labViewData[i]!=','){
// read stbd motor direction
stbd_direction=stbd_direction+labViewData[i];
}else if(commaCount==3 && labViewData[i]!=','){
// read port motor direction
stbd_speed=stbd_speed+labViewData[i];
}
}
// change data from string to integer
portDirection=(int)port_direction.toFloat();
portSpeed=(int)port_speed.toFloat();
stbdDirection=(int)stbd_direction.toFloat();
stbdSpeed=(int)stbd_speed.toFloat();
// map 0-10 LabVIEW speed command to 0-255 (8 bit PWM)
portSpeed = map(portSpeed, 0, 10, 0, 255);
stbdSpeed = map(stbdSpeed, 0, 10, 0, 255);
}
// Run port and stbd motor control function, see end of loop() for fuction definations
control_port(portDirection,portSpeed);
control_stbd(stbdDirection,stbdSpeed);
//Store current feedback, see end of loop() for fuction definitions
portCurrent=get_port_current();
stbdCurrent=get_stbd_current();
// reset variables
commaCount=0;
port_direction="";
port_speed="";
stbd_direction="";
stbd_speed="";
portCurrent=0;
stbdCurrent=0;
}
//Definations of functions that controls the motors
void control_port(int port_d, int port_s){//Controlling Port Motor (M2)
if(port_d){
digitalWrite(EN, HIGH); //Turn off sleep mode
digitalWrite(M2_IN1, HIGH); //FWD signal HIGH
digitalWrite(M2_IN2, LOW); //REV signal LOW
analogWrite(M2_D2, port_s); //Set speed
Serial.print("Port FWD, speed: ");
Serial.println(port_s);
}else{//if FALSE REV, and sets speed
digitalWrite(EN, HIGH); //Turn off sleep mode
digitalWrite(M2_IN1, LOW); //FWD signal LOW
digitalWrite(M2_IN2, HIGH); //REV signal HIGH
analogWrite(M2_D2, port_s); //Set speed
Serial.print("Port REV, speed: ");
Serial.println(port_s);
}
}
void control_stbd(int stbd_d, int stbd_s){//Controlling Stbd Motor (M1)
if(stbd_d){//if TRUE FWD, and sets speed
digitalWrite(EN, HIGH); //Turn off sleep mode
digitalWrite(M1_IN1, HIGH); //FWD signal HIGH
digitalWrite(M1_IN2, LOW); //REV signal LOW
analogWrite(M1_D2, stbd_s); //Set speed
Serial.print("STBD FWD, speed: ");
Serial.println(stbd_s);
}else{//if FALSE REV, and sets speed
digitalWrite(EN, HIGH); //Turn off sleep mode
digitalWrite(M1_IN1, LOW); //FWD signal LOW
digitalWrite(M1_IN2, HIGH); //REV signal HIGH
analogWrite(M1_D2, stbd_s); //Set speed
Serial.print("STBD REV, speed: ");
Serial.println(stbd_s);
}
}
//Function defination for getting current feedback
int get_port_current(){
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(M2FB) * 9;
}
int get_stbd_current(){
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(M1FB) * 9;
}
