Hi everyone!
I’ve finished building my A4WD rover and tested it out using the three GP12D IR sensors and everything works fine. Now I’m working on my own code to autonomously operate the rover using a Ping sensor on a panning servo. I haven’t really started working on the actual code yet but, I’ve been playing around with the sensors and the motors seperately just to get a feel for the system. All of my sensor experiments have been going as planned but, the codes I’ve written to run the motors hasn’t been working. This is a code I’ve been playing around with to run the motors.[code]
LSpeed var word
RSpeed var word
minspeed con 1750
maxspeed con 1250
LSpeed = 1500
RSpeed = 1500
low p0
low p1
sound 9, [100\880, 100\988, 100\1046, 100\1175]
main
; Numbers lower than 1500 result in forward direction.
; Numbers higher than 1500 result in reverse direction.
LSpeed = (LSpeed - 10) min maxspeed ;accelerates the motors
RSpeed = (RSpeed - 10) min maxspeed
pause 2000
LSpeed = (LSpeed + 10) max minspeed
RSpeed = (RSpeed + 10) max minspeed
pause 2000
; Send out the servo pulses
pulsout 0,(LSpeed2) ; Left Sabertooth channel.
pulsout 1,(RSpeed2) ; Right Sabertooth channel.
pause 20
goto main
[/code]
The code is supposed to run the motors forward for two seconds then run them backwards for two seconds. Instead of doing this however they go forward and well just keep on going forward. I have no doubt that my nooby coding just isn’t working well but, I can’t think of any way to fix the problem so any help would be appreciated.
hiiii
we can’t program the motors to stop but we can program the motors to go forward
so we need a program for the motor to go forward then stop after a certain time
You will need additional restructuring of the code - perhaps two separate loops - to allow it to go forward, pause and then go in reverse. The pause commands where you have them won’t do the trick, since I do not see where you are outputting the pulses during acceleration to the pins. Unfortunately, the way I read it I must be missing something because it looks like it would decrement the speeds by 10, then increment them by 10 (back to 1500) and then output your pulses - which with a value of 1500 would not move the motors. Perhaps someone could explain to me the “max variable” and “min variable” options?
dj
Caveat: This is from my general programming knowledge, and not specifically for your platform. I too, am trying to learn.
The code as written will not accelerate the motors. So you must be seeing the motors turn on hard. Right?
What processor and IDE are you using? I think you may have bad values for the processor you are using. Also the code adds and subtracts from the values before doing the pulsout, so yeah, your code is the problem.
actually the code is not working well
as i tried it too but it doesnt work well
so plz i need a sample program to run the motors forward then
to stop it after a predetermined time
LSpeed var word
RSpeed var word
giddyup var word
minspeed con 2000
maxspeed con 1000
LSpeed = 1500
RSpeed = 1500
low p0
low p1
sound 9, [100\880, 100\988, 100\1046, 100\1175]
main
; Numbers lower than 1500 result in forward direction.
; Numbers higher than 1500 result in reverse direction.
for giddyup = 1 to 50
LSpeed = (LSpeed - 10) min maxspeed ;accelerates the motors forward
RSpeed = (RSpeed - 10) min maxspeed
gosub dosumpthin
next
pause 2000 ;bot is moving
for giddyup = 1 to 50
LSpeed = (LSpeed + 10) max minspeed ;decell to stop
RSpeed = (RSpeed + 10) max minspeed
gosub dosumpthin
next
pause 2000 ;bot is stopped
for giddyup = 1 to 50
LSpeed = (LSpeed + 10) max minspeed ;accelerates the motors reverse
RSpeed = (RSpeed + 10) max minspeed
gosub dosumpthin
next
pause 2000 ;bot is moving
for giddyup = 1 to 50
LSpeed = (LSpeed - 10) min maxspeed ;decell to stop
RSpeed = (RSpeed - 10) min maxspeed
gosub dosumpthin
next
pause 2000 ;bot is stopped
goto main
Dosumpthin:
; Send out the servo pulses
pulsout 0,(LSpeed*2) ; Left Sabertooth channel.
pulsout 1,(RSpeed*2) ; Right Sabertooth channel.
pause 20
return
Thanks! Thats exactly what I wanted it to do. Now the only problem is that I feel lazy that I didn’t even make my own code. I’ll see if I can post a video once my rover is finished. It might be a LONG time though.
A coding clarification, please… Why did his original code go hard forward only? I ask because it looked to me since it was starting at 1500, decrementing by 10 and then incrementing by 10 (resulting in 1500) - the pulse sent should not move the motor at all?
All I can say is it didn’t act that way for me. I didn’t use a motor controller. I just used a couple servos to see what pulses were being generated. It sent 1500uS pulses every 2 seconds just as it should have.
I guess you never know what can happen when you have someone playing with technology that they can only half way operate.
I now understand the value of having subroutines and I see where I was messing up. I didn’t know that you had to send out a servo pulse every time you wanted the motors to do something different. I also have a better understanding of the incrementing and the decrementing. One clarification though. What is the meaning for the for next loops. I don’t really understand their purpose.(I’ll try and see what the rover does without the for next loops just to see of I can figure it out on my own.)
Also, the long term plan on my rover is to make a code that periodically stops the motors and scans the ping sensor with the panning servo. It will then act accordingly and scan again and so on.
for giddyup = 1 to 50
LSpeed = (LSpeed - 10) min maxspeed ;accelerates the motors forward
RSpeed = (RSpeed - 10) min maxspeed
gosub dosumpthin
next
The for command sets up a loop. So the value of the variable giddyup is 1 on the first pass, then 2, 3, etc. The next command looks at the value and directs the loop back to the beginning, until the value is equal to 50, then it breaks out of the loop and control goes to the next command in the program. Something like that.
So, its the for next loop that controls how long the motors run rather then the pause. I’ve already learned a lot more from you in a couple of days than I have studying the BAP manual for months. Thanks for all the help.
I finally found out that actually the motors neutral position on my robot for some reason or another are 1492 rather than 1500. I have absolutely no idea why.
Often motor controllers use a ceramic resonator for the oscillator instead of a more accurate (more expensive) crystal like is used on the microcontrollers that send data to the motor controller. This means the motor controller is trying to stop at 1500uS, but it’s not very accurate. So in your case it was 1492uS. The good news is once you find the value it will not change.
Update!
I’ve finished building the obstacle avoidance code for my rover and it’s working pretty well. Now I’m trying to fine tune it some. However, its hard to fine tune the code when my rovers batteries are running out of charge constantly causing how hard the rover turns and how for it goes to vary. When I bought my rover kit I neglected to buy one of lynxmotions rechargable batteries and instead I’ve been using two 9 volt batteries. I’ve been thinking of using an RC truck battery that is a 19.2 volt NiCd. I fear this may be too much of a power overdose and I need someone with more experience to help me.
Thanks in advance.
Ok, I’ve thought through it myself(found it was a stupid question ) and 19.2 volts is pretty much suicidal, right. So I just made an order of batteries and a couple other minor SES and cable parts that I needed. I’ll probaboly have a video by next week.