Important changes to forums

We’re making some changes to the Mbed forums. From 10th December 2019 all new discussions will take place on our new forum site. You can continue to reply to existing threads for the next two weeks. After that we will archive this forum so you can return to useful posts in the future.

PwmOut not working for pins?

05 Jul 2011

This is my code

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
PwmOut motor1(p21);
PwmOut motor2(p22);
PwmOut led1(LED1);
PwmOut led2(LED2);

float speed = 0.0;

void motorcontrol(float speed);

int main() {
    pc.printf("Press 'u' to turn motor clockwise, 'd' to turn it counter clockwise\n");
    motor1.period(0.01);
    motor2.period(0.01);
    while(1) {
        pc.putc(speed);
        char c = pc.getc();
        if((c == 'u') && (speed < 1)) {
            speed += 0.05;
            motorcontrol(speed);
        }
        if((c == 'd') && (speed > -1)) {
            speed -= 0.05;
            motorcontrol(speed);
        } 
        
    }
    error("Loop unexpectedly terminated");
    return 0;
}

void motorcontrol(float speed) {
    if (speed>0) //clockwise
    {
    motor1=1;
    motor2=1-speed;
    led1=speed;
    led2=0;
    }
    if (speed<0) //ccw
    {
    motor1=1+speed;
    motor2=1;
    led1=0;
    led2=-speed;
    }
    if (speed==0) //brake
    {
    motor1=1;
    motor2=1;
    led1=0;
    led2=0;
    }
}

When I run this, the LED brightness changes as expected, but when I measure the pin outputs using an oscilloscope, I get weird readings. Does anyone know what could be causing this problem?

Edit Also, would something like following work?

PwmOut pin (p21)

int main(){
pin=0.5;
}

In other words, does the pin=0.5 need to be placed in a infinite loop for it to work?

05 Jul 2011

What sort of motors are you trying to drive with the PwmOuts ? Or maybe I should ask what are you driving the pins into that are controlling the motors?

05 Jul 2011

An H-Bridge (TLE5206) with output logic exactly mirroring that of the input.

05 Jul 2011

Hmm, I've written a couple of motor driver libraries for RC servos and stepper motors (see on my profile page) but neither of these appear useful for driving a TLE5206.

I'll have a go later today at writing a library for the TLE5206 from the datasheet.

However, I don't have a TLE5206 device (or motors) to test it, all I have to go on it a scope to look at the signals. But that should be enough, I'll post back here when I have something to try out.

But right now, here in the UK it's 4am and I need some bed time :)

05 Jul 2011

OK, here it is. Note, I haven't made this public as yet so you will need to use the following links.

Once you are happy it works as expected let me know and I'll tidy up the code, add more documentation and publish it as public.

Note You are strongly advised to test this using a scope before you attach the TLE5206 and try to drive the motor for real. I didn't have a device to connect to, just a scope. It looks like what the datasheet specs out but it would be best that it looks like what you are expecting before driving potentially expensive hardware (esp hardware that moves under power such as motor).

Import librarySimpleTLE5206

Tripple Controller for the TLE5206 H Bridge motor controller

Please ensure you read the example1.h file first so as to see how to setup a typical demonstration.

05 Jul 2011

Wow Andy, you're amazing. When I posted the question, I was expecting one or two line answers, not for a complete program! I won't have time to test it today but I will tomorrow and let you know how it went. Thanks!

05 Jul 2011

Well Andy, it works! I can't thank you enough!

05 Jul 2011

Btw, it says in there that all motors need steady accel/decel and you even included this in your code. Since I'm building a self balancing robot, I think the responsiveness of the motors will be quite important. What do you think a reasonable number for the poll_interval will be? Will 1ms be okay for most motors?

Also, from what I understand, motor perform best when the pwm freq is around 10khz. Do you know if this is true and whether if there will be detriments when I do this?

fyi, I have changed the pwm freq to 10khz and the poll interval to 50us (=20khz).

Thanks

05 Jul 2011

Quote:

Since I'm building a self balancing robot...

This changes things. It's not speed control you are after, it would appear to be position control. A linear accel profiler I did isn't going to get close to what you need. The poll interval isn't the issue, it's the accel_rate value that is. The problem is that this is basically just a P controller (proportional) where the "off balance" is a feedback signal that represents the error in the system. Using a P controller wont give you the stability you need.

What you need is something a little more sophisticated and you should probably be looking at the PID controller. Take a read of that link. It also refers you to other really good sites on the subject of closed loop control systems.

Have a read and if you need more help post back and I'll try and assist where possible.

06 Jul 2011

I see. I understood that I needed a PID controller eventually, but first I just wanted to be able to generate the pwm output, which is why I started this thread. If we go back to basics, should the following generate a pwm signal with the default 50Hz frequency and 50% duty cycle?

PwmOut In1(p21);

int main() {
  while(1){
  In1=0.5
  }
}
06 Jul 2011

PwmOut In1(p21);

int main(){
    In1.period_ms(20);// 1 sec div by 20ms is 50Hz 
    In1.pulsewidth_ms(10);//On time of 10ms so 50% duty cycle
}
22 Apr 2016

thank you Tony Cheng for your code of july, 5th 2011

You need to log in to post a reply