United Kingdom
Asked — Edited
Resolved Resolved by Rich!

Setting Max Speed For H-Bridge Motors

Hi there.

So, I have 2 drive motors running through a L298 H-bridge. I have set these up with a "H-bridge with speed control" Movement Panel and all is working well, but here's where I need a little guidance.

At the moment the motors are controlled via joystick 1. I have the variable speed box checked and the sensitivity set right down to the lowest option in the joystick 1 config menu. Problem is it is still quite sensitive so I need to reduce the maximum motor speed by at least half when the joystick is fully pressed. Is this achievable, and if so how do I go about doing this? I'm guessing there is some scripting involved somewhere. I have had a look through the script menu but I don't know what I need to use. I will also need a reduced maximum speed for these motors for when for when things go autonomous when I get round to setting that up.

Many thanks,

Steve.


ARC Pro

Upgrade to ARC Pro

Subscribe to ARC Pro, and your robot will become a canvas for your imagination, limited only by your creativity.

#1  

PWM is how you control H-bridge speed, and like many answers lately, the init script is your friend :)

Take a look at the Roli Example Project init script and H-bridge Movement Panel for sample of how to set this up. (the init script sets the speed to maximum, but would be easy to modify).

Alan

#2  

I forgot you can also use SetSpeed script command for more granular control. This is actually what the speed sliders in the H-bridge Movement Panel control. So your init script sets pwm of both motors to 100, then use setspeed to set a value between 1 and 255.

United Kingdom
#3  

@thetechguru.

Thanks for the advice. Yeah I have been using the forum a lot this week. Got a week off so making the most of it ironing out a few issues. Anyway, I had a look at the Roli example and got the following script and changed the values...


:loop
# This sets the speed for the HBridge Motor Controller
# The speed is a number between 0 and 255
# Setting this speed will initialize the PWM on the motor controller
SetSpeed(100)

# Even though we set the speed, we will also enable the PWM
# on the speed pins that connect to the HBridge Motor Controller
# The number is 100, which means 100%
pwm(d4, 500)
goto(loop)

I'm still in the very early stages of learning my scripting skills so I probably have missed something as what's above made no difference to the motor's speed. What am I missing? confused

United Kingdom
#4  

Quote:

PWM (digitalPort, speed)

     Set the PWM (Pulse Width Modulation) to the desired duty percentage cycle

     This simulates voltage on the specified pin (Between 0 and 5v)

     PWM Value is between 0 and 100

     Example: PWM(D14, 90)

In your example you have PWM set to 500. Change it to 50 and see what happens.

As far as using the joystick to vary the speed but limit it to half speed that would require the custom Movement Panel and some scripting as the H-Bridge with PWM control panel doesn't have an upper/lower limit config option (possible improvement for a future update there though).

The joystick can give, if enabled, variables for joystick 1 x and y positions, these can be used to specify the PWM. The Y value is 0 when centered, 1 when fully forward and -1 when fully backward.

So at 50% forward it should read 0.5. This can be used with simple math to calculate the PWM required. Set that to a variable and use in a script to set the PWM.

I'm not home nor do I have ARC at the moment and with a script like this I'd prefer to use ARC rather than my memory so can't provide an example but it's pretty straight forward logic. Think about each thing you have, how you want them to work and look at what commands are available and it should come to you.

For the math, it's something simple like PWM = Joystick1Y * 10 for forwards. Reverse would need to be changed from a negative number to a positive number so PWM = (Joystick1Y * -1) * 10

United Kingdom
#5  

Oops, that should have been 50 not 500. Tried it again and it worked briefly, but now I get a console message saying "joystick already running" and promptly disconnects me from the EZ-B.


:loop
# This sets the speed for the HBridge Motor Controller
# The speed is a number between 0 and 255
# Setting this speed will initialize the PWM on the motor controller
SetSpeed(100)

# Even though we set the speed, we will also enable the PWM
# on the speed pins that connect to the HBridge Motor Controller
# The number is 100, which means 100%
pwm(d4, 50)
goto(loop)

I know I've added a loop, but without it the script makes no difference.

#6  

This code should not be in a loop. You set it and forget it.

Thwre should be two PWM statements unless you soldered the wire from d4 to be both pwm pins on the H-bridge. There is a pwm pin for each motor.

The setspeed controls both pwm pins unless otherwise specified.

You should not need to reduce both the setspeed and the pwm. Set pwm to 100 on both just to make sure pwm is initialized, then use setspeed to actually control your speed using a value between 1 (slowest) and 255 (highest).

The sliders on the side of the H-bridge Movement Panel adjust setspeed after the initial setting, and they control it per motor (so if your motors are not spinning at identical speed you can adjust as needed).

Alan

#7  

The pwm pins on the l298n are labeled ena and enb, not pwm... What do you have d4 connected to?

Alan

United Kingdom
#8  

The speed control pins ena and anb are both connected to D4 with a Y wire.

(EDIT). I've just set the PMW to 100 as suggested and tried different values in the setspeed. The sliders did change possition but motors still go at full speed when joystick is fully pressed.

United Kingdom
#9  

I thought something similar to this might work (but didn't have much luck).


:loop
IF ($Joysticky1 = 1)
setspeed(50)
sleep(1000)

ELSEIF ($joysticky1 = -1)
setspeed(50)
sleep(1000)
ENDIF
sleep(1000)
goto(loop)

#10  

You are going to make me plug in a joystick to help figure this out (I have just been using the Movement Panel and the arrow keys on my keyboard, and the touch movement object, not an actual joystick).

I'll play around tonight or this weekend to see if using a joystick changes anything.

Alan

United Kingdom
#11  

Use PWM() rather than SetSpeed()

However that wont give analogue speed control. You need to throw some math at it.


:loop

IF ($Joysticky1 = 1)
  PWM(D4,50)
ELSEIF ($joysticky1 = -1)
  PWM(D4,50)
ENDIF

sleep(1000)

goto(loop)

Or with the math


:loop

# Check and convert negative number to positive if needed
IF($joysticky1 < 0)
  # If negative multiply by -1 to change to positive
  $PWMSpeed = $joysticky1 * -1
Else
  # If positive just save the value
  $PWMSpeed = $joysticky1
EndIf

# Convert the value to usable PWM value
$PWMSpeed = $PWMSpeed * 50
# Make it an integer
$PWMSpeed = Round($PWMSpeed, 0)

# Set the PWM
PWM(D4,$PWMSpeed)

# Sleep
sleep(50)

# Loop back
goto(loop)

United Kingdom
#12  

@the Techguru.

Cheers Alan. Got to get this sorted before K-9 runs in to someone and breaks their ankle eek.

@Rich.

Oh no, not maths. I hate maths confused. Jokes aside, I'll have a look at that script a bit later and see if that will work for me. cheers.

#13  

If all else fails, you could use a custom Movement Panel and have the forward command turn on the forward trigger ports, and set the pwm to 50.

#14  

@Rich, Just curious why you say not to use SetSpeed. It is working well for setting the speed of my Roli. Or are you trying to have the speed increase the further the joystick is pushed, in which case modifying the PWM may be the way to go.

@Steve G. Everything we solve for you will be used in my Steampunk K9 project, so better for me if we get it sorted now rather than after I start my build (particularly since I expect I will be using wheelchair motors and a Sabertooth since I expect my bot to be very heavy, so I want absolute control over his speed and direction to prevent killing someone or breaking furniture.....)

I am basically using Roli as a test bed for the bigger project.

Alan

United Kingdom
#15  

Yes, faster speed the further the stick is pushed. SetSpeed() should work too but it requires the Movement Panel to be set up correctly, I'm not convinced that in this case it is.

United Kingdom
#16  

Just to confirm, the H-bridge Movement Panel is set up correctly. I changed the nessasery servo ports and when using the joystick, the wheels/motors do exactly what they should. The joystick I'm using does support vairable speeds and it does work. It's just to sensitive even with sensitivity turned right down (to 5 or 0.5 whatever the minimum is). just need to reduce the overall max speed.

#17  

Just to be sure. You are using H-bridge PWM Movement. In settings Left and Right PWM are both set to D4?

On the Movement Panel itself, on the right side, there are two slide controls. These set the max speed of each PWM channel (which are the same for you since you have the same port connected to both).

If you are just using an H-Bridge Movement Panel, it doesn't allow PWM control in the panel, just in scripting, and may not achieve what you want.

Alan

United Kingdom
#18  

Yup. Using the PWM panel with sliders.

#19  

OK. I'll get a joystick plugged in tonight or this weekend and see if I can get it figured out.

United Kingdom
#20  

Nice one. Thanks Alan. ;)

#21  

I am not sure how much help I am going to be. I plugged in my joystick, an old Microsoft Sidewinder USB joystick, and it did exactly what it was supposed to do. Pushing the stick a little set the speed controls to low, and Roli crawled away slowly. Pushing the stick further, brought the speed up to max and he took off down the hall. Same thing with each direction. This joystick has a wide range of motion, so the speed changes were very smooth.

One thing I did notice, is in the joystick control, there is a checkbox to "use variable movement speed". do you have that checked?

However using the H-bridge movement panel, I could not set a maximum speed. The joystick over-rode the speed I had set in my init script as soon as I pushed it to maximum.

I think for setting a maximum speed you may need a custom movement panel, and something similar to the sabertooth ramping scripts that Rich wrote for Gwen's project. Scripts attached to the Sabertooth Ramping project on EZ-Cloud (it will obviously need to be modified to send the correct commands to the H-bridge instead of the SendSerial() commands it uses now).

Either that, or get a joystick with a wider range of motion so that you have more control. What brand/model joystick are you using?

Alan

United Kingdom
#22  

Thanks for trying it out Alan. Yup, that was exactly the same thing that was happening to me, with the joystick over-riding the init script I had. Yes I do have the vairable speed tick box checked. The brand of joypad I'm using is a CSL C210 wireless pad.

I'm having a little with the drive motors at the moment, where the drive along for a short while then quickly gradually lose power, I wait a few seconds, then they drive normal and slowly lose power again, so I think my 12v drivetrain battery is knackered which is now making testing a little difficult (I think that's the cause anyway). Just ordered a new 11.1v LiPo which should be with me Tuesday so I'll carry on experimenting then and keep you informed.

#23  

OK. I have some other stuff I want to work on this weekend, and can't dedicate the whole weekend to robotics, but if I have some time I will try to adapt the Sabertooth Ramping project to work with an L298N with PWM speed control instead.

This will actually be useful for my Roli as well. I have a little pocket size BT joystick and it doesn't have proportional output on the direction buttons. They just emulate hitting the arrow keys on the keyboard, so if I create this custom Movement Panel and script, it will work for me too.

Alan

United Kingdom
#24  

Well after tring a few script examples I'm still not having much luck solving this. I've come across a new issue here which is not helping matters. The couple of things I tried mentioned in this thread still moves the sliders in the Movement Panel back to full. Really not sure what to do now. confused

#25  

I haven't had as much free time as I thought, and had some other priorities with my Roli, but this is still on my todo list. I am certain it is solvable with a custom Movement Panel and scripting similar to what we did with the Sabertooth Ramping scripts Rich wrote for Gwen's B9.

Now, since it turns out you need a more powerful H-Bridge, anything I come up with will need to be adapted to the commands for that bridge, but we'll cross that bridge when we get there (or if you decide to bite the bullet and get a Sabertooth, things are much simpler. It turns out Rich's ramping script was superfluous because you can hook the Saberooth up to a TTL adapter and give it a command to tun auto-ramping on, and we can make a custom Movement Panel that has whatever you want as your max speed be the command that gets sent to move in that direction).

Alan

United Kingdom
#26  

I might be looking to get a sabertooth early next year if there's no other way. For now I've just ordered a 10amp H-bridge which should solve my other problem.

I was thinking, all we really need for this is a setting in the Movement Panel config menu, where we move a slider control to where we want it set, and tick a check box next to it saying "use this slider setting for the max speed". Unchecked, the slider/speed would go back to default maximum. Something DJ may consider adding to his to do list. Just a thought to make things a little easier perhaps. ;)

#27  

That tends to be what happens here. Several of us will start discussing how to achieve something with existing features, and just about the time we have it worked out, DJ will add it as a feature.

I think it is one way he judges the priority of things. The more we are willing to work for it, the more likely it is that other customers will want it too.

Alan

United Kingdom
#28  

I'm more than certain that an adaptation of the custom Movement Panel on Gwen's B9 would achieve what is needed here.

Just refresh my memory (without me re-reading the topic). What's the end goal? What must be achieved?

#29  

Goal: Conrinue to allow speed ramping when using a Joystick to control an HB 298n, but be able to set a max speed lower than speed(256) or pwm(100). Today, the Joystick over-rides the speed setting in the Hbridge with PWM movement panel.

Should be modifiable to work with the new H-Bridge steve ordered due to the 298n not being sufficient for his motors.

Alan

United Kingdom
#30  

Well I got my motor controller and have just installed, had a play around with the joystick again trying to finally sort out the max speed issue, and finally solved it with Rich's help in another thread in post 4 . ;)

I opened a Modified control panel and inserted the following scripts in to the config menu.


To Stop...

Set(D1, On)
Set(D3, On)
PWM(D0, 0)
PWM(D2, 0)

To go forwards...

Set(D1, On)
Set(D3, On)
PWM(D0, 60)
PWM(D2, 60)

and to go in reverse...
Code:
Set(D1, Off)
Set(D3, Off)
PWM(D0, 60)
PWM(D2, 60)

This works like a charm. I haven't added left of right code as I have a separate servo for this. Hopefully this will help you out too Alan (let me know how you get on), and thanks again to Rich for your help :). I just need to stop the wheels spinning when I power the EZB and waits to make a connection, which I have asked about here in post #15.

#31  

Oh, that was much simpler than I expected.
[edited to remove question that made no sense once I opened ARC and looked at the options]

Alan

United Kingdom
#32  

Much simpler than I expected too :). Only thing is that I have lost vairable speed from the joystick, but Rich did leave a script with some math to deal with this which I may play around with.