Cannot create a loop with servo commands

Blazemaguire

Senior Member
Hi,


I've hacked an old school robot we had lying around (from 'scantek2000' if anyone else has seen one before) to run of a PICAXE 18x.

It has 4 futabu servo's controlling Jaw, base, elbow and arm movement.

I've written the simple code below (please see my previous posts.... i'm a newbie to writing BASIC code!) just to test the range of movements and to build from.

It works fine in that it directs the robot to all the extremeties of movement, only it only runs through the routine once - It then just stops, even though there is a 'goto main' command.

I'm fully aware it could be something wrong with my code, but does anyone have any suggestions as to what I could do to get it to repeat on a loop?

Rob



symbol time=500


main:
goto init

goto jaw_open
goto Jaw_closed

goto elbow_Fully_up
goto elbow_fully_down
goto Shoulder_Fully_up
goto Shoulder_Fully_down
goto Base_Turn_Right
goto Base_Turn_Left
goto main




init:
servo 0,150
servo 1,150
servo 2,150
servo 3,150


Jaw_closed:
servopos 0,75
pause time



Jaw_open:
servopos 0,225

pause time


Elbow_Fully_Up:
servopos 1,75
pause time

Elbow_Fully_Down:
servopos 1,225
pause time



Base_Turn_Right:
servopos 2,75
pause time


Base_Turn_Left:
servopos 2,225
pause time


Shoulder_Fully_Up:
servopos 3,75
pause time


Shoulder_Fully_Down:
servopos 3,225
pause time
 

hippy

Ex-Staff (retired)
You need a GOTO at the end of the program to wherever you want to start from again; "GOTO main", "GOTO init", etc
 

Blazemaguire

Senior Member
Thanks!

I thought that the 'goto main' at the end of the main routine would do that... but your quite right, i've put a goto main at the end of the entire program and it now works!

Rob
 

Blazemaguire

Senior Member
Now that that works and I've had a good play, I have another question!

Does anybody have any easy method for controlling the 'traverse' rate of the servos.

For instance, I can switch from fully left, to fully right on the 'base' servo of the robot, but it swings so violently and with so much inertia that it nearly falls over! - ruining any accuracy it might have had in the process (also i'm sure its not good to run the servos 'flat out' all the time for their life expectancy!)

Is there any way of keeping the same movment routines as in the code above, but just slowing them down so it moves from command to command at a more managable speed.

I know it could be done by moving in small increments say:

servopos 0, 100
pause 1
servopos 0, 101
pause 1
servopos 0, 102
pause 1 ....... all the way up to the desired max limit, but that would be a massive programming effort to get a sequence of movements happening

Is there a routine, or BASIC command / method I could use to simplify this intention and slow down the movements?

My eventual aim is to make the robot 'teachable' in that you move it to postiions from an 'input joystick' and it records the movements into memory and will then repeat the excercise autonomously. - I'm developing it as a teaching aid for my year 8 Systems and Control lessons...

I'm sure its possible with BASIC, its just my knowledge and experience in code programming is lacking (i'm learning quick though!)

Rob
 

eclectic

Moderator
"servopos 0, 100
pause 1
servopos 0, 101
pause 1
servopos 0, 102
pause 1 ....... all the way up to the desired max limit, but that would be a massive programming effort to get a sequence of movements happening
"unquote

How about a For- Next loop?

Code:
for b0 = 100 to 200
servopos 0,b0
pause 10 ; or whatever you require
next
See Manual 2 p.52 and 177
for more details
e
 

inglewoodpete

Senior Member
I too have been struggling with how to get a smooth(er) movement using several servos for a human-like figure. I have tried code like what eclectic suggests but there is a lot of juddering. One option we are considering is using jelly rubber mounting for the servos. If the limbs etc have a bit of mass, the inertia of the steps is absorbed by the rubber. The jelly rubber will also provide some cushioning for the servo, hopefully extending its life.
 

MPep

Senior Member
@Blaze,

If you had written the original program using GOSUBs instead of GOTOs, and ended each SUBroutine with a RETURN, then the original code would have worked.
See the following
Code:
#picaxe 18X

symbol time=500


main:
gosub init

gosub jaw_open
gosub Jaw_closed

gosub elbow_Fully_up
gosub elbow_fully_down
gosub Shoulder_Fully_up
gosub Shoulder_Fully_down
gosub Base_Turn_Right
gosub Base_Turn_Left
goto main

init:
    servo 0,150
    servo 1,150
    servo 2,150
    servo 3,150
return

Jaw_closed:
    servopos 0,75
    pause time
return

Jaw_open:
    servopos 0,225
    pause time
return

Elbow_Fully_Up:
    servopos 1,75
    pause time
return

Elbow_Fully_Down:
    servopos 1,225
    pause time
return

Base_Turn_Right:
    servopos 2,75
    pause time
return

Base_Turn_Left:
    servopos 2,225
    pause time
return

Shoulder_Fully_Up:
    servopos 3,75
    pause time
return

Shoulder_Fully_Down:
    servopos 3,225
    pause time
return
Using the simulator, this loops forever.
 
Last edited:

BeanieBots

Moderator
For smooth variable speed servo movements, have a look at the code here:-
http://www.picaxeforum.co.uk/showthread.php?t=7760

That code was written before servopos was available.
It should be possible to change it to use servopos without too much effort but very fine changes to rate of change of position can be obtained by sticking with pulsout and making changes to the frame rate.

Alternatively, get a servo contoller and send it a rate command!
 
Top