Jump to content
Sign in to follow this  
NathanPiasco

[MSP432] Servo librairy conflicting with PWM analogWrite

Recommended Posts

Hi,

 

Sorry for the repost, I save my preview topic in the wrong place I think (http://forum.43oh.com/topic/9286-msp432-servo-librairy-conflicting-with-pwm-analogwrite/, if someone can delete this one...).

 

I am using an MSP432P401R launchpad Rev B with Energia 17 on Windows 10.

I've recently got this issue: I can't control servomotors with the Servo.h librairy and at the same time send several PWM.

As soon as I called the "attach" function from Servo, the others call to analogWrite in my code don't work anymore.

I think it is due to some timer conflicts (both functions using A0 timer ?), but I can't figured out how to fix the problem.

I see something wierd in the Servo librairy code (in Servo.cpp:72):

//timerParams.clockSource = Timer_Source_SMCLK;

This instruction, that seems to choose the timer to use, is commented. Moreover I noticed that several defined are not used in the code of Servo (from Servo.h):

#define SERVO_TIMER               TIMERA0_BASE
#define SERVO_TIME_CFG            TIMER_CFG_B_PERIODIC | TIMER_CFG_SPLIT_PAIR
#define SERVO_TIMER_TRIGGER       TIMER_TIMB_TIMEOUT
#define SERVO_TIMER_INTERRUPT    INT_TIMERA0B
#define SERVO_TIMER_AB            TIMER_B
#define SERVO_TIMER_PERIPH        PRCM_TIMERA0

I have tried to modify "A0" by "A1" without success.

 

Did someone run in a similare problem and could give me some clue to fix it ?

 

Thanks

 

Nathan

Share this post


Link to post
Share on other sites

The piece of code you point to is actually not used in the implementation. It is a left over from a previous implementation. I will remove it to avoid confusion in the future.

Servo picks a timer automatically from the pool of timers to allow maximum flexibility. Unfortunately this does not feedback into analogWrite. analogWrite might use the same timer as the Servo does and thus causing a conflict. I'll look into if there is a way to avoid this conflict.

 

In the meantime, which pins are you using for analogWrite? Can you share your Sketch so that we can have a look at it in detail?

 

Robert

Share this post


Link to post
Share on other sites

Hi Robert,

 

Thank you for your answer. Here the servo code:


volatile int i = 90; 
volatile boolean raise = true; 

void setup_turret() 
{
  T0.attach(turretPin_0);  
  T1.attach(turretPin_1);  
  
  T0.write(90);  
  T1.write(90); 
}

void loop_turret()
{
  
  if ((i < T1_cmd_max)&&(raise))
  {
    T1.write(i);  
    i = i + 5;
  }
  else if ((i >T1_cmd_min)&&(!raise))
  {
    T1.write(i);  
    i = i - 5;
  }
  if (i == T1_cmd_min)
    raise = true; 
  if (i == T1_cmd_max)
    raise =false; 
  #if DEBUG_TURRET 
    Serial.print("T0 : ");
    Serial.print("90");
    Serial.print(" \t ");
    Serial.print("T1 : ");
    Serial.print(i);
    Serial.println(" ");
  #endif  
  delay(100);
}

We control two motors using a H bridge, so we send four PWMs. Here a code used for debuging:

 analogWrite(motor_R_Forward, 255); //v_R_for);  // ~490 Hz
 analogWrite(motor_R_Backward, 0); //v_R_back);  // ~490 Hz
 analogWrite(motor_L_Forward, 255); //v_L_for);  // ~490 Hz
 analogWrite(motor_L_Backward, 0); //v_L_back);  // ~490 Hz

And the file used to define pin numbers:

// Common Libraries #include <math.h>#include <Servo.h> ///////////////// Debug Prints /////////////////#define DEBUG_DODGE 0 // Print dodge results#define DEBUG_MOTOR1 1 // Print motor command sent (-1 -> 1)#define DEBUG_MOTOR2 1 // Print motor command sent (back 0->1, front 0->1)#define DEBUG_US 0    // Print distance detected by US sensors #define DEBUG_PHOTO 0 // Print Command send by photosensors #define DEBUG_SETPOINT 0 // Print vector command sent#define DEBUG_TURRET 0 // Print agnle sent to turret///////////////////// Pins /////////////////////// Motor Pins #define motor_R_Forward 31 // P3_7 #define motor_R_Backward 32  //P3_5 #define motor_L_Forward 4 //P3_3#define motor_L_Backward 3  //P3_2// turret pins #define turretPin_0 18 //P3_6#define turretPin_1 11 //P3_0// Turret parameters Servo T0;                    // create servo object to control down servo Servo T1;                    // create servo object to control up servo #define T1_cmd_min 30        // (

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×