Hello,
I am developing a basic differential drive robot with Arduino and I am facing a problem when the code starts implementing the interrupts never getting out of it again.
the interrupt is to get the encoder ticks for the DC motors so I am assuming that since the wheels will continue moving the trigger pin will get high always and being stuck in the callback function I want to know is this the situation, if so how can I use the interrupts properly
the values of the encoder ticks will be sent to through serial port to ROS to be processed
here is my code
// *********************************************** //
void setup() {
setupROS();
setupEncoders();
setupMotors();
}
// *********************************************** //
void setupROS(){
nh.initNode();
nh.subscribe(sL);
nh.subscribe(sR);
nh.advertise(pL);
nh.advertise(pR);
}//end of the setup ROS function
void setupEncoders(){
pinMode(Left_Encoder_PinA, INPUT_PULLUP);
pinMode(Left_Encoder_PinB, INPUT_PULLUP);
//Attaching interrupt in Left_Enc_PinA.
attachInterrupt(Left_Encoder_PinA, do_Left_Encoder, RISING);
pinMode(Right_Encoder_PinA, INPUT_PULLUP);
pinMode(Right_Encoder_PinB, INPUT_PULLUP);
//Attaching interrupt in Right_Enc_PinA.
attachInterrupt(Right_Encoder_PinA, do_Right_Encoder, RISING);
}// end of the setup Encoders Function
void setupMotors(){
pinMode(A_L,OUTPUT);
pinMode(B_L,OUTPUT);
pinMode(PWM_L,OUTPUT);
pinMode(A_R,OUTPUT);
pinMode(B_R,OUTPUT);
pinMode(PWM_R,OUTPUT);
}// end of the setupMotors Function
// *********************************************** //
void loop() {
do_Left_Encoder();
do_Right_Encoder();
moveLeftMotor(left_motor_speed);
moveRightMotor(right_motor_speed);
nh.spinOnce();
}// end of the main function
// ********************************************** //
//############################################################//
void moveLeftMotor(float PWM_val){
if (PWM_val>0)
{
digitalWrite(A_L,HIGH);
digitalWrite(B_L,LOW);
analogWrite(PWM_L,PWM_val);
}
else if(PWM_val<0)
{
digitalWrite(A_L,LOW);
digitalWrite(B_L,HIGH);
analogWrite(PWM_L,abs(PWM_val));
}
else if(PWM_val == 0)
{
digitalWrite(A_L,HIGH);
digitalWrite(B_L,HIGH);
}
}
//############################################################//
//############################################################//
void moveRightMotor(float PWM_val){
if (PWM_val>0)
{
digitalWrite(A_R,HIGH);
digitalWrite(B_R,LOW);
analogWrite(PWM_R,PWM_val);
}
else if(PWM_val<0)
{
digitalWrite(A_R,LOW);
digitalWrite(B_R,HIGH);
analogWrite(PWM_R,abs(PWM_val));
}
else if(PWM_val == 0)
{
digitalWrite(A_R,HIGH);
digitalWrite(B_R,HIGH);
}
}// end of the move right motor function
//############################################################//
//############################################################//
/* void do_Right_Encoder()
{
if (digitalRead(Right_Encoder_PinB) == HIGH) {
if (digitalRead(Right_Encoder_PinA) == LOW) {
Right_Encoder_Ticks++;
} else {
Right_Encoder_Ticks--;
}
} else {
if (digitalRead(Right_Encoder_PinB) == LOW) {
Right_Encoder_Ticks--;
} else {
Right_Encoder_Ticks++;
}
}
//enRticks.data = Right_Encoder_Ticks;
//pR.publish(&enRticks);
}// end of the do right encoder function
//############################################################//
//############################################################//
void do_Left_Encoder()
{
// Test transition; since the interrupt will only fire on 'rising' we don't need to read pin A
if (digitalRead(Left_Encoder_PinB) == HIGH) {
if (digitalRead(Left_Encoder_PinA) == LOW) {
Left_Encoder_Ticks++;
} else {
Left_Encoder_Ticks--;
}
} else {
if (digitalRead(Right_Encoder_PinB) == LOW) {
Left_Encoder_Ticks--;
} else {
Left_Encoder_Ticks++;
}
}
enLticks.data = Left_Encoder_Ticks;
pL.publish(&enLticks);
}// end of the do left encoder function */
//############################################################//
void do_Left_Encoder()
{
// Test transition; since the interrupt will only fire on 'rising' we don't need to read pin A
LeftEncoderBSet = digitalRead(Left_Encoder_PinB); // read the input pin
Left_Encoder_Ticks -= LeftEncoderBSet ? -1 : +1;
enLticks.data = Left_Encoder_Ticks;
pL.publish(&enLticks);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//do_Right_Encoder() Definitions
void do_Right_Encoder()
{
RightEncoderBSet = digitalRead(Right_Encoder_PinB); // read the input pin
Right_Encoder_Ticks += RightEncoderBSet ? -1 : +1;
enRticks.data = Right_Encoder_Ticks;
pR.publish(&enRticks);
}