Using Cogs Properly for motor control
Litefire
Posts: 108
I'm using the Prop in a robot i'm trying to build. i've got a homebuilt H-Bridge (functioning perfectly) that is directly controlled by PWM for speed control. the issue is that there are two motors, and thus it needs to be in parallel. this is my first foray into parallel processing on the prop (very exciting), but my problem right now is that i can't figure out how to:
a) set up a program so it constantly updates the required speed of the motor
b) be able to send speed values to the specific controls
thanks!
a) set up a program so it constantly updates the required speed of the motor
b) be able to send speed values to the specific controls
thanks!
Comments
The Green and Blue lines go from an I/O pin to my H-Bridge. Depending on which one is active, the motor runs forward or backward. both motors can be engaged (in one direction, of course) at once. activating both fwd and rev for one motor starts to seriously cook the transistors (i learned that today!).
I need a program that can control the PWM output to the signal lines of the H-Bridge. I need the reaction to be as quick as possible, and to be able to run/control both motors simultaneously.
Thanks so much for your help!
~~Brian
I need a program that can control the PWM output to the signal lines of the H-Bridge. I need the reaction to be as quick as possible, and to be able to run/control both motors simultaneously.
Thanks so much for your help!
~~Brian
EDIT: Link was broken, apparently i didn't use it as an attachment... oops.
There is a lot of information available from Parallax on the Propeller and how to use it. You should familiarize yourself with the relevant web pages and their contents including the "sticky threads" at the beginning of this forum.
perhaps there's a way to manipulate that example to export the PWM signal on the direction pin? and how would i use that to control both motor simultaneously?
thanks!
~~B
Also, don't let the inputs to the Hbridge have a potential float state, use a 10k to GND on all Hbridge inputs as pull down in case the micro is in a weird state.
The question is, what method are you using for PWM on each pin? Knowing that can help find a best method to control the 4 pins to your existsing Hbridges. The PWM object is great. You just run a loop updating the 4 variables for duty(assuming the cycle length can always remain the same).
here is just an idea, you probably need 4 cogs in reality, and the loop just updates the 4 methods containing separate PWM objects.
Post Edited (originator) : 3/19/2007 3:29:29 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life
I assume that i need two of these (one per motor) to run the device? And, i know this is a newbie question, but how do i integrate this with my main code? Thanks so so much!
When i get out of class later today i'll see if that works!
~~Brian
~~Brian
just got a chance to run your program. i looks like there's some form of undefined symbol ("addr" in the :loop section of the DAT block)
what should i change it to? i have no assembler experience...
~~B
I thought I got them all. Sorry. Please change them all to "temp" instead of "addr" and try again.
Mike
any ideas on what might be wrong? here's what i have now.
as noted above, motorstest.spin is the top file.
~~Brian
In the assembly code, I (correctly) assumed it was the pin number. Now it's corrected. Change the
name of the file by deleting the "-1" from the name before compiling.
where pwm is your MotorPWMTwo object. again, nothing happens.
when i drive the PWM pins high manually, the motors work perfectly (just in case you were wondering...) so it's not a hardware issue.
~~Brian
edit: same as below. the [noparse][[/noparse]one] is not appearing when i post.
Post Edited (Litefire) : 3/19/2007 8:54:19 PM GMT
i tried the PWM method in the BS2_functions object and it works great. but here's something weird.
when i do this:
the right motor goes perfectly at half-power.
do this:
perfect, this time the left motor activates
but try this:
and only the right motor activates.
confused...
~~Brian
p.s. the index for the second instance of bs2 doesn't seem to be appearing... it's in my code though.
I'm not sure what's going on. Here's a correction of some minor errors that wouldn't explain what's happening.
Maybe someone else experienced in assembly can jump in and look at what I did. There must be something simple
I'm forgetting. Sorry.
seems to be working just fine all on it's own now!
thanks so much! they should change your tag from "Registered Member" to full-fledged "Guru"
~~Brian
i'm thinking that it'd look like this:
just as a quick idea i wrote that in this quick reply box :P. but where should i put that? it would be excellent if there could be some active section monitoring and updating speeds to keep the robot going straight. because right now i don't think the motors run at exactly the same speed (inherent of DC motors they run different speeds forward and reverse and we've got them mounted so one goes forward and one runs in reverse...).
~~Brian
I'd probably try to find a way to build this into the setMotor routine since that runs by itself in a cog and is already responsible for the actual motor speed. In a way, now the setMotor routine blindly sets the expected motor speed as a % duty cycle. Instead, it could use the encoder outputs for feedback on the actual motor speed. I couldn't say offhand how or where to put the code, but, conceptually, this makes sense to me.
as usual, if i have questions i'll put them here. if it works i'll repost the object to the exchange. with all due props of course.
edit: to save a look at the manual, how do i set minimum and maximum limits on variables?· equivalent to MIN and MAX on the BS2.
~~Brian
This may speed up the PID learning
~~Brian
In you case you seem to be more wanting to synchronize two motors, although they are getting the same PWM, may have a drift due to motor fluctuations. This would require the encoders as you stated to check distance travelled on one motor compared to a second motor.
So just thinking out loud, you have a distance error between the motors, either negative or positive, depending on who is ahead in the race. Either way, it is a distance calculation, which really is an ERROR to correct. In my limited experience with PID, there may actually be several approaches. One is, an accurate measure of RPM, and correction. The second is the difference between the encoder counts. That difference is the error. So, lets imagine a loop doing nothing but comparing the quad encoder counts. MotorLeftCount - MotorRightCount = MotorError. So a variable is updated many times a second with the resulting error. That variable is an Offest to one of the motors, I see no need to compensate both motors right off hand.
Forgetting PID for a second, which may even be overkill, if the Error is 50 counts, to correct, the left motor PWM increases duty by a ratio to catch up, if the left motor overshoots the speed(count) of the right, then the error is negative, and a negative compensation kicks in to offset the difference. I would start there first, see if there is an obvious way to add that to your existing code. Of course this means that the motors need to not always run at 100% duty, as there is no margin to over-power one to motor to "catch" up. This may not need some fancy PID calculation, but it may need some way to "smooth" the compensation to avoid constant overshooting back and forth, maybe not. Phil P posted a formula in a thread in the sandbox, I think is was by Owen, the subject was "joystick dampening math/ code", there may be some useful material there for the correction.
Hope ths helps.
An oversimplification:
Post Edited (originator) : 3/21/2007 3:14:59 AM GMT
But the right wheel's encoder seems to be working. so as a proof of concept thing it's working just fine.
~~Brian
~~Brian