Newbie Alert - Motor Speed/Direction Control Probs

markhuntxxy

New Member
Appologies first for the stupid question, but been pulling my hair out for a while on this and don't seem to be getting anywhere!!
The main problem is my lack of understanding of how to control the speed and direction of 2 motors on my PICAXE MICRO-ROBOT. I've read, re-read and read again the AXE120 manual (Section 9) but still faill to get my robot to follow a line in a controlled manner. The manual says I should use a pulsout command but I don't seem to be having any success.
Can I use this "pulsout" command to set the forward speed also?
The code I am using is below, but I am obviously making a blunder somewhere! I'm using a picaxe 28 with line follower board, using inputs 1,2 and 6. The Motors are controlled by outputs 4,5,6and7.
Please help!!!:confused:


init:
pause 100 'motor controller start up pause

let b1=pin1
let b2=pin2
let b6=pin6

main:
if pin1 = 1 then go_forward
if pin2 = 1 then go_left 'turn left
if pin6 = 1 then go_right 'turn right
goto go_stop


go_forward:
Pause 100 'Pause for 100ms
let pins = %10100000 'go forward
goto main

go_left:
Pause 100 'Pause for 100ms
let pins = %00110000 'go left
pulsout 6,50 'pulses Left Speed

goto main

go_right:
Pause 100 'Pause for 100ms
let pins = %11000000 'go right
pulsout 4,50 'pulses Right Speed

return

go_stop:
Pause 100
let pins = %00000000 'stop - not over line!
goto main
 

hippy

Technical Support
Staff member
I'm not familiar with the AXE120 but the first step is probably to get motor control working and understood before trying to control it with line following. Likewise, getting light/line detection proven working before trying to use it. With both together it's not easy to say which of the two ( or both ) are not working correctly.

Start with the MicroBot wheels up then write a few programs to make the motor run at certain speeds, then move on to changing speeds, speeding up slowing down, stopping and starting. Forward and Reverse if it does that. Conversely, try different PULSOUT values and see what it achieves.

Once you have one motor working you can write some code which makes both work together. Have it move forward, turn, move forward again and see if you can make it go in a square, figure-8 and so on.

Take it a step at a time and it should all become much more understandable and you can build on what you're learning and discovering rather than just diving in. The manual will start to make a lot more sense when you can see how what it says relates to what actually happens.

It may sound like it will take a long time to do this, and nowhere close to what you eventually want to do, but once you get over any initial hurdles and it starts to make sense, it's all plain sailing from there.
 
Last edited:

BeanieBots

Moderator
Very sound advice from Hippy as always.
I'd also suggest restricting yourself in the way you are controlling the outputs, at least for now.
Forget about "let pins = " and pulsout. It will only confuse.
Stick with the "high" and "low" commands to start with.
Then determine which of the motor control pins does what.

For example:-
Try each of these sets of four commands to see what they do

Low 4
Low 5
Low 6
Low 7
'all motors should be off

Then set each output high in turn with the other three outputs low
High 4
Low 5
Low 6
Low 7
'one motor should turn in one direction

Low 4
High 5
Low 6
Low 7
'the same motor should turn in the other direction
'then do the same with outputs 6 & 7.

Also, looking at your code, the first thing you do in each of your motor control routines is pause. You really want the pause to be AFTER you have changed the outputs that control the motors.
Think it through and it makes sense.
If you are giving instructions to somebody, you would normally say "do this, for this amount of time" rather than "keep doing what you are for this amount of time and then do this". The difference is quite subtle but normally you would want your robot to react as quickly as possible to the sensors rather than wait for a bit before responding.
 

hippy

Technical Support
Staff member
Another good idea is to define the pin settings from Section 9 as SYMBOL constants and that makes them much easier to use ...

Code:
Symbol BUGGY_STOP       = %00000000
Symbol BUGGY_FORWARD    = %10100000
Symbol BUGGY_TURN_LEFT  = %10010000
Symbol BUGGY_REVERSE    = %01010000
Symbol BUGGY_TURN_RIGHT = %01100000

Do
  pins = BUGGY_FORWARD   : Pause 1000
  pins = BUGGY_STOP      : Pause 1000
  pins = BUGGY_REVERSE   : Pause 1000
  pins = BUGGY_STOP      : Pause 1000
  pins = BUGGY_TURN_LEFT : Pause 1000
  pins = BUGGY_STOP      : Pause 1000
Loop
 

Rickharris

Senior Member
Your robot uses a motor driver chip (L293) to provide the high current the motors need - This is controlled by the picaxe (ignore the PWM for now.)

The 293 is an H bridge and can control 2 motors. Each motor is controlled by 2 outputs from the picaxe 18
4 -5 control one motor and 6 - 7 the other.

If one output is high the motor goes forward and if the other is high it goes back.

If both outputs are low the motor stops.

If both outputs were to go high the motor would try to back and forward at the same time so the 293 has internal circuits to prevent this and both outputs high will also stop the motor.

So as BB says simple high and low will drive the motors.

The robot steers by stopping one motor whilst keeping the other going so it turns towards the stopped motor. (like a tank steers by stopping one of the tracks)

To make the robot turn sharper you can have one motor going forward and the other going backwards - the robot will turn on the spot.

The PWM (Pulse width modulation) effects the speed of the motors by turning them off and on very quickly - If the motors are on half the time and off half the time the robot will go half as fast as full on all the time.

Section 10 has a sample programme that should be your starting point for exploring how this robot works so copy that first.

Have fun PS you could build more of these your self for around £2 to £7 for the bits now you know how it works.

Code:
symbol speedR = b1
symbol speedL = b2
pause 100 ‘ motor controller start-up pause
main:
let speedR = 255 ‘ maximum speed
let speedL = 255 ‘ maximum speed
gosub set_speed ‘ set the speed
let pins = %10100000 ‘ buggy forward
loop:
if input6 is on or input2 is on then stop
goto loop ‘ loop around
stop:
let pins = %00000000 ‘ stop
let speedL = 60 ‘ set slow speed
let speedR = 60 ‘ set slow speed
gosub set_speed ‘ set the speed
let pins = %01010000 ‘ reverse
pause 3000 ‘ wait 3 seconds
let pins = %10010000 ‘ turn
pause 2000 ‘ wait 2 seconds
goto main ‘ loop
set_speed:
let pins = %00110000 ‘ set left speed
pulsout 6,speedL ‘ send a pulse of length in ‘speedL’
pause 10 ‘ short delay
let pins = %11000000 ‘ set right speed
pulsout 4,speedR ‘ send a pulse of length in ‘speedR’
pause 10 ‘ short delay
return
 
Last edited:
Top