Award
Congratulations Rich, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Setting Max Speed For H-bridge Motors

Assistance Requested

Help Steve G with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

Steve G claims to have checked these sources:
 
#1

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.

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.


Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


#2

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

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

#3

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.

#4

@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...

Code:


: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*

#5

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 EZ-Builder at the moment and with a script like this I'd prefer to use EZ-Builder 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

#6

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.

Code:


: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.

#7

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

#8

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

Alan

#9

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.

#10

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

Code:


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

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