Forum >DF 4WD Combining encoder code with motor code
DF 4WD Combining encoder code with motor code

How do I combine the following two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders?
I have successfully used the following code to run my DF Robot 4WD platform through a series of movements:
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control HIGH = reverse left side
// LOW = forward left side
int M2 = 7; //M2 Direction Control HIGH = forward right side
// LOW = reverse right side
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void setup(void)
{
pinMode(5, OUTPUT);
Serial.begin(19200); //Set Baud Rate
}
void loop(void)
{
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(10000);
stop();
delay(3000);
analogWrite (E1,190); // slight right turn
digitalWrite(M1,LOW);
analogWrite(E2, 70);
digitalWrite(M2,HIGH);
delay(2800);
stop();
delay(3000);
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(12000);
stop();
delay(4000);
}
I have since added encoders to the platform and would like to use the following code to control these encoders:
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction
void setup()
{
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module
}
void loop()
{
Serial.print("Pulse:");
Serial.println(duration);
duration = 0;
delay(500);
}
void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, wheelSpeed, CHANGE);
}
void wheelSpeed()
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
}
}
encoder0PinALast = Lstate;
if(!Direction) duration++;
else duration--;
}
As I said above...My question is how do I combine these two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders? When I upload the encoder code by itself, nothing happens to the robot. I assume I need to somehow mesh these two sets of code together, but I don't know how.
I have successfully used the following code to run my DF Robot 4WD platform through a series of movements:
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control HIGH = reverse left side
// LOW = forward left side
int M2 = 7; //M2 Direction Control HIGH = forward right side
// LOW = reverse right side
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void setup(void)
{
pinMode(5, OUTPUT);
Serial.begin(19200); //Set Baud Rate
}
void loop(void)
{
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(10000);
stop();
delay(3000);
analogWrite (E1,190); // slight right turn
digitalWrite(M1,LOW);
analogWrite(E2, 70);
digitalWrite(M2,HIGH);
delay(2800);
stop();
delay(3000);
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(12000);
stop();
delay(4000);
}
I have since added encoders to the platform and would like to use the following code to control these encoders:
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction
void setup()
{
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module
}
void loop()
{
Serial.print("Pulse:");
Serial.println(duration);
duration = 0;
delay(500);
}
void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, wheelSpeed, CHANGE);
}
void wheelSpeed()
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
}
}
encoder0PinALast = Lstate;
if(!Direction) duration++;
else duration--;
}
As I said above...My question is how do I combine these two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders? When I upload the encoder code by itself, nothing happens to the robot. I assume I need to somehow mesh these two sets of code together, but I don't know how.
2012-01-18 20:15:25 Thanks for this code...I haven't uploaded it yet, but will do so soon. I'm glad to have some code from which to start.
Thanks,
Thad
Thad
Thanks,
Thad

2012-01-17 23:40:38 Hi,
Here is another sample code that might help you on your way.
Hector
Here is another sample code that might help you on your way.
Code: Select all
// # // # Editor : Lauren from DFRobot // # Date : 17.01.2012 // # Product name: Wheel Encoders for DFRobot 3PA and 4WD Rovers // # Product SKU : SEN0038 // # Description: // # The sketch for using the encoder on the DFRobot Mobile platform // # Connection: // # left wheel encoder -> Digital pin 2 // # right wheel encoder -> Digital pin 3 // # #define LEFT 0 #define RIGHT 1 long coder[2] = { 0,0}; int lastSpeed[2] = { 0,0}; void setup(){ Serial.begin(9600); //init the Serial port to print the data attachInterrupt(LEFT, LwheelSpeed, CHANGE); //init the interrupt mode for the digital pin 2 attachInterrupt(RIGHT, RwheelSpeed, CHANGE); //init the interrupt mode for the digital pin 3 } void loop(){ static unsigned long timer = 0; //print manager timer if(millis() - timer > 100){ Serial.print("Coder value: "); Serial.print(coder[LEFT]); Serial.print("[Left Wheel] "); Serial.print(coder[RIGHT]); Serial.println("[Right Wheel]"); lastSpeed[LEFT] = coder[LEFT]; //record the latest speed value lastSpeed[RIGHT] = coder[RIGHT]; coder[LEFT] = 0; //clear the data buffer coder[RIGHT] = 0; timer = millis(); } } void LwheelSpeed() { coder[LEFT] ++; //count the left wheel encoder interrupts } void RwheelSpeed() { coder[RIGHT] ++; //count the right wheel encoder interrupts }
