Jump to content
43oh

IR library and PWM


Recommended Posts

Ok, I am out of ideas on this.  I am going to start by saying, this works on an arduino uno, no problems.

 

Then follow that up by saying this works perfectly on a launchpad with a 2553 in it IF I DO NOT USE THE IR LIBRARY.

 

 

So this is what happens:

 

Simple motor control for a two motor robot.  Using the IR Library to receive commands from a remote control.

 

I upload the code, and I can make both motors go full throttle, forward, or backward.  The moment that I try to spin right or left, I get a quick "jerk" from both motors, in opposite directions (as I would expect) and then nothing...I can not even make it go forward and backward anymore until I reset the launchpad.

 

Again, if I hook the launchpad up, and give it a routine for the robot (forward, spin, backward, spin, left, right, etc) it works just perfectly fine.  It is the addition of the IR Library and whatever it is either doing with timers, or interrupts...but I just can't seem to figure it out.

 

I have tried every combination of PWM capable pins that I can think of, and can not get it to work.

 

Here is the code.  I would sincerely appreciate any help you can give me.

 

//=======================================================================
//  IR Remote Control Robot
//  Remixed by Jerod W.
//=======================================================================

#include <IRremote.h>
#include <IRremoteInt.h>
  
//motor control pin
const int E1=4;    //motor 1 (right) enable pin
const int M1A=2;   //motor 1 (right) Output pin 1
const int M1B=3;   //motor 1 (right) Output pin 2
const int E2=13;    //motor 2 (left) enable pin
const int M2A=5;   //motor 2 (left) Output pin 1
const int M2B=7;   //motor 2 (left) Output pin 2


  
//IR Receiver Module Pin and variable
int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;
  
//  setup function
//=======================================================================
void setup()
{
//configure all motor control pin as output
pinMode(E1,OUTPUT);
pinMode(M1A,OUTPUT);
pinMode(M1B,OUTPUT);
pinMode(E2,OUTPUT);
pinMode(M2A,OUTPUT);
pinMode(M2B,OUTPUT);



  
//disable both motor by default
analogWrite(E1,0);
analogWrite(E2,0);
  
//start IR receiver
irrecv.enableIRIn();
}
  
//  loop function
//=======================================================================
void loop()
{
//IR signal received
if(irrecv.decode(&results))
{
//forward (Up arrow)
if(results.value==0x820 || results.value == 0x20)
{
motor(255,255);
}
//reverse (VOL-)
else if(results.value==0x821 || results.value == 0x21)
{
motor(-255,-255);
}
//rotate left (PREVIOUS)
else if(results.value==0x811 || results.value==0x11)
{
motor(240,-240);
}
//rotate right (NEXT)
else if(results.value==0x810 || results.value==0x10)
{
motor(-240,240);
}
//forward left (POWER)
else if(results.value==0x80F || results.value==0xF)
{
motor(230,160);
}
//forward right (FUNC/STOP)
else if(results.value==0x80C || results.value==0xC)
{
motor(180,220);
}
//reverse left (DOWN)
else if(results.value==0x80D || results.value==0xD)
{
motor(-230,-160);
}
//reverse right (UP)
else if(results.value==0x822 || results.value==0x22)
{
motor(-180,-220);
}

//receive the next value
irrecv.resume();
  
//short delay waiting for repeating IR signal
// (prevent it to stop if no signal received)
delay(50);

}
//no IR signal received
else
{
  
//right wheel stop
analogWrite(E1,0);
  
//left wheel stop
analogWrite(E2,0);

}
}

  
//  extra function
//=======================================================================
  
//function to control the motor
void motor(int left, int right)
{
//limit the max speed
//if(left>255)left=255;
//else if(left<-255)left=-255;
//if(right>255)right=255;
//else if(right<-255)right=-255;
  
//left wheel forward
if(left>0)
{
//left wheel direction forward
digitalWrite(M2A,LOW);
digitalWrite(M2B,HIGH);
//left wheel speed
analogWrite(E2,left);
}
//left wheel reverse
else if(left<0)
{
//left wheel direction reverse
digitalWrite(M2A,HIGH);
digitalWrite(M2B,LOW);
//left wheel speed
analogWrite(E2,-(left));
}
//left wheel stop
else
{
//left wheel stop
analogWrite(E2,0);
}
  
//right wheel forward
if(right>0)
{
//right wheel direction forward
digitalWrite(M1A,LOW);
digitalWrite(M1B,HIGH);
analogWrite(E1,right);
}
//right wheel reverse
else if(right<0)
{
//right wheel direction reverse
digitalWrite(M1A,HIGH);
digitalWrite(M1B,LOW);
analogWrite(E1,-(right));
}
//right wheel stop
else
{
//right wheel stop
analogWrite(E1,0);
}
}

 

 

Link to post
Share on other sites

IRremote lib uses monopolizes Timer1 and analogWrite() does the same for some pins. 

 

For the MSP430G2553 pins 4, 14 and 19 use Timer0 and you could use 2 of those except that all these pins are on the same PWM channel and thus would result in e.g. the same PWM signal on pin 4 while doing an analogWrite() to pin 14.

 

The only way around this would be to make IRremote use Timer0. This is pretty easy. Simply replace all occurrences of TA1xxx with TA0xxx in the file IRremoteInt.h.

Link to post
Share on other sites

Also, let me make sure that I understand.  Based on what your post said...if I change the IR Lib to use Timer0, then I should be able to get independent PWM from 2.3, and 2.5?  Since 2.3 is on T1A1 and 2.5 is on T1A2?  Am I understanding that correctly?

Link to post
Share on other sites

I guess you're getting it right yeah.

Most value line MSP430 (like those with the launchpad) are quite limited on timers, there are tricks to fix this, but you'd need to go in depth to do that.

Energia abstracts these quirks away, as long as the used libraries do not conflict with eachother, which unfortunately often happens.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...