-deleted-
6 years, 6 months ago.

Struggling with basic code for a servo motor

So im currently doing a project and have just kinda been thrown into using an the NUCLEO-F401RE board for a buggy. This buggy requires the use of a servo motor and at the moment im just trying to have full access of all angles using a pwm signal, I have attached this very basic code below and can only get the motor to change from 0 degrees to 180 degrees changing the value of ' mypwm.pulsewidth_ms();' from 1 to 2. Unfortunately it has been quite some time since I have used c coding and was never that great at it in the first place and am completely inexperienced with what I am doing at the moment. So I am just trying to have full access of all the angles and have tried using decimal places like 1.5 but this just would round down to 1 hence an angle of 0 degrees. I have tried troubleshooting this code with the use of floats and various other websites I have found online with no luck, as a novice I am struggling and if anyone could help me I would be extremely grateful. Thank you to anyone who bothers helping in advance although im kinda doubting anyone will. 

code:

  1. include "mbed.h"

PwmOut mypwm(PA_7);

int main() {

mypwm.period_ms(20); mypwm.pulsewidth_ms(1);

}

3 Answers

6 years, 6 months ago.

Jason,

Which servo are you trying to control?

Some servos only have binary angle control, meaning you can only set them to 0 or 180 degrees. Other servos don't even have a full 180 degree rotation. Either way, a spec sheet or part # of the servo you are trying to use could clear things up.

The code you have is good to go for a general case. You should be able to change the pulse width from a range of integer values starting at 1ms, to whatever your period is (in this case, 20ms). The duty cycle then corresponds to a specific angle and will set your servo to that angle.

6 years, 6 months ago.

Not sure if you're aware of this page: https://os.mbed.com/components/cat/actuators/ Might have some examples that work out for you. Any of the NXP examples should port easily to the Nucleo.

6 years, 6 months ago.

Try the following code, you can use the member function period_us instead of period_ms so you can resolve pulsewidths less than 1ms.

#include "mbed.h"
//Program to 'sweep' test a 'standard RC type servo
//Define some parameters using compiler directive '#define'
//Check Servo DATA if 0.75ms to 2.25ms then use min=750 and max=2250
//NB be values in microseconds (Following are generic values)
#define MID         1500
#define MIN         1000
#define MAX         2000
#define STEP          50
//Time delay between steps in milliseconds
#define TIME         100

DigitalOut myLed(LED1);
DigitalIn  myButton(USER_BUTTON);

PwmOut myServo(PA_7);

int main() {
    
    myServo.period_ms(20);
    myServo.pulsewidth_us(MID); //NB in microseconds

    while(true) {
        for (int i=MIN;i<=MAX;i+=STEP){
            myServo.pulsewidth_us(i);
            wait_ms(TIME);
        }
        for (int i=MAX;i>=MIN;i-=STEP){
            myServo.pulsewidth_us(i);
            wait_ms(TIME);
        }
    }
}

Hope this helps,/explains