Putting robotics at your service™

Free shipping on orders over $200

Random Servo pulse generator code.

Print view Share :
Previous topicNext topic

Page 1 of 2 [ 23 posts ]

1, 2
User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-05-21 00:01 
Using the atom 28 or pro28 how can i program random Servo movements..
with ..
limits on the move delay (time between moves) say 3-10 seconds.
limits on the travel time (speed) say up to 3 seconds
limits on the servo range (angle) say +/- 30°?

Thanks in advance guys.

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Guru ( offline )
Posts: 4127
Posted: 2012-05-21 00:20 
Generate a random number scaled to the desired range.

Alan KM6VV

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-05-21 17:14 
Humm, yes thats what i need help with.
Just wondered if there was any examples about?

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-06-12 11:07 
The plan is to either use the BA28-P28 or just a SSC-32 if it can work as a stand alone controller only controlling up to say 8-9 servos?
Id like to switch on the power then have the servos moving randomly, but id also like to individuality limit the servos travel, end times, speed and even maybe how frequently they are commanded as explained before.
Is there any way of doing this?

Its always these simple programming bits that get me. :oops:

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Guru ( offline )
Posts: 9256
Posted: 2012-06-12 11:44 
Random is well, random... What you probably want is a pseudo random set of values to send to a movement system.

Each channel would have a set of values for:

Delay between moving.
Movement speed.
Movement position.

Each value would have a valid range defined.

Now you have some rules for the "randomness" and you can code it. I would seed the random number generator with a value based on a timer and a switch press. After the numbers are gathered you simply mathify them to be in the required range. You could then wrap it all up in a program that randomizes which servo to move.

_________________
Jim Frye, the Robot Guy
http://www.lynxmotion.com
I've always tried to do my best...


User avatar
Guru ( offline )
Posts: 9256
Posted: 2012-06-13 14:41 
he Atom Pro's random function generates a 32 bit (long) from the seed value. The manual suggests basing the random number on a timer (timer A), then use that number as a seed for the final random number.

Code:
a var long
 a = random TCA
 a = random a


You can wrap this in a subroutine to get a new random number each time it is entered. Now we need to scale the results to usable values. We tried it with this code. Note, I see a lot of duplicates...

Code:
a var long 
main 
 a = random TCA 
 a = random a 
 serout s_out, i38400, [dec a, 13]
pause 20
goto main


66358
131538
130
10
196862
131406
522
131538
130
65846
132050
642
522
131538
131918
30
132050
642
66358
131538
130
10
196862
131406
522
131538
131918
30
132050
642
522
131538
131918
30
132050
131406
542
131526
130
10
132050
131406
542
131538
130

However changing the code to this.

Code:
a var long 
 a = random TCA
main
 a = random a 
 serout s_out, i38400, [dec a, 13]
pause 20
goto main


Results in values like this.

1023555307
2048158937
4113160612
3931353674
3568788615
2826881296
1374458402
2732139607
1169377456
2339869026
402596566
787432639
1557039488
3096253186
1880761879
3760540721
3242956916
2189832682
68969414
154715807
309431617
618863252
1238840363
2461952344
611177138

So now if any of the real smart guys wants to jump in... ;)

_________________
Jim Frye, the Robot Guy
http://www.lynxmotion.com
I've always tried to do my best...


User avatar
Guru ( offline )
Posts: 9256
Posted: 2012-06-13 15:23 
My first gut instinct tells me to convert the numbers to a fraction and multiplying by the range.

The values:

1023555307
2048158937
4113160612

Convert to:

.1023555307
.2048158937
.4113160612

Then for servo position you do this math.

Code:
servo_position = (rand_no x 2000) + 500


The conversion would be something like this.

Code:
while rand_no > 1
? bit shift ?


Riley and Devon suggested modulus but I don't understand it.

Code:
servo_pos = (rand_no // 2000) + 500


Helping? :P

_________________
Jim Frye, the Robot Guy
http://www.lynxmotion.com
I've always tried to do my best...


User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-06-13 15:59 
thanks for taking the time posting that.
Robot Dude wrote:
Riley and Devon suggested modulus but I don't understand it.


lol, i don't really understand any of it. lol

iv been writing up routines like:

Code:
;GOSUB Simulations
FastBlink
hservo [BlinkPin\-11500\0]
pause 50
hservo [BlinkPin\0\0]
return

SlowBlink
hservo [BlinkPin\-11500\100]
pause 1000
hservo [BlinkPin\0\100]
return

SlowNeckRotateLeft
hservo [NeckRotate_Pin\10000\100]
return

SlowNeckRotateRight
hservo [NeckRotate_Pin\-10000\100]
return

OpenWingsSlow
hservo [LWingPin\10000\100]
hservo [RWingPin\10000\100]
return

OpenWingsFast
hservo [LWingPin\10000\0]
hservo [RWingPin\10000\0]
return

CloseWingsSlow
hservo [LWingPin\-10000\100]
hservo [RWingPin\-10000\100]
return

CloseWingsFast
hservo [LWingPin\-10000\0]
hservo [RWingPin\-10000\0]
return

PitchWingsUPSlow
hservo [WingsPitchPin\10000\255]
return

PitchWingsUPFast
hservo [WingsPitchPin\10000\0]
return

PitchWingsDOWNSlow
hservo [WingsPitchPin\-10000\255]
return

PitchWingsDOWNFast
hservo [WingsPitchPin\-10000\0]
return

BeakSnap
hservo [BeakPin\10000\0]
pause 100
hservo [BeakPin\0\0]
return


Using the Hservo Driver:
Code:
 hservo [BlinkPin\         (BlinkPosition1*stepsperdegree1)/100 + Blink_Offset\_mBlinkSpeed, |
           RWingPin\        (RWingPosition1*stepsperdegree1)/100 + RWing_Offset\_mRWingSpeed, |
           WingsPitchPin\    (WingsPitchPosition1*stepsperdegree1)/100 + WingsPitch_Offset\_mWingsPitchSpeed, |
           LWingPin\        (LWingPosition1*stepsperdegree1)/100 + LWing_Offset\_mLWingSpeed, |
           BeakPin\            (BeakPosition1*stepsperdegree1)/100 + Beak_Offset\_mBeakSpeed, |
           Neck_UpperF_Bpin\ (Neck_UpperF_BPosition1*stepsperdegree1)/100 + Neck_UpperF_B_Offset\_mNeck_UpperF_BSpeed, |
           Neck_UpperL_Rpin\ (Neck_UpperL_RPosition1*stepsperdegree1)/100 + Neck_UpperL_R_Offset\_mNeck_UpperL_RSpeed, |     
           Neck_LowerF_Bpin\ (Neck_LowerF_BPosition1*stepsperdegree1)/100 + Neck_LowerF_B_Offset\_mNeck_LowerF_BSpeed, |           
           Neck_LowerL_Rpin\ (Neck_LowerL_RPosition1*stepsperdegree1)/100 + Neck_LowerL_R_Offset\_mNeck_LowerL_RSpeed, |
           NeckRotate_pin\    (NeckRotate_Position1*stepsperdegree1)/100 + NeckRotate_Offset\_mNeckRotateSpeed]

return


But again im just playing with code already available.

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Expert ( offline )
Posts: 253
Posted: 2012-06-13 17:46 
Just typed this up in notepad, downloaded the studio and tested it. This code spits out random servo pulses ranged 500 - 2500.

Code:
randn var word
a var word
a = random TCA

range var word
minvalue var word

;define the working range you want
range = 2000

;define the smallest number in the range
minvalue = 500


main
gosub Gen_random
serout s_out, i38400, [dec randn, 13]
pause 100
goto main

Gen_random
   a = random a
   randn = (a // range) + minvalue
return

_________________
Devon Simmons, Former Programmer for Lynxmotion.
I can try to help, but I can only offer you my best effort.


User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-06-13 18:37 
Oh devon. You are a genius. Thank you.
Now to order another board and chip. Lol

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Guru ( offline )
Posts: 2120
Posted: 2012-06-13 19:20 
Agree, that is really a brilliant way to solve this issue.

For better understanding of the modulo operation these two lines give the same result (as long as a/range return an integer):

Code:
randn = (a // range) + minvalue
   randn = (a - (range * (a/range)))+ minvalue


If a = 1999 the remainder would be this value since the division of 1999/2000 return 0, so the result would be 2499 (1999+500).

This mean that if you want the range to be exactly 500 - 2500 the range variable should be set to 2001 and minvalue = 500.

The reason for why I'm posting this is that I didn't understand the modulo // operation either.

Thanks to Devon for learning us programming smarter! :D

_________________
Kåre Halvorsen, Zenta
-----------------------------------------
Zenta's blog
http://zentasrobots.com/
Zenta's YouTube channel
http://www.youtube.com/ZentaOlbaid
-----------------------------------------


User avatar
Guru ( offline )
Posts: 4921
Posted: 2012-06-13 20:25 
Yep - The fun part at times is getting it random from one run to another. I see he initialized the first seed to TCA which should hopefully be reasonably sufficient, but as the program takes more or less the same time to start up from one run to the next could potentially have very similar results from one run to the next...

On the Arduino, one approach they mention is to use an analogRead of an unconnected IO pin as the seed. randomSeed(analogRead(0));

Also they already built the range code into it for you. If you want values 500-2500 (both inclusive), you could simply do:
myval = random(500, 2501);


Kurt


User avatar
Guru ( offline )
Posts: 3301
Posted: 2012-06-14 04:01 
I also suspect the same thing can be used for random speed and random servo movement delays?
Some of the code ill be using canned sequences and its great to be able to have random movements running along side this. Thank you.

_________________
Jonny Poole,
Freelance Concept Art & Animatronics, 3D Art and 3D Modelling, as well as Physical Modelling, Mechanical design, and illustration.
http://www.innerbreed.co.uk
Create, Innovate, Inspire.


User avatar
Guru ( offline )
Posts: 2041
Posted: 2012-06-14 06:20 
wisper (botborduino)

_________________
Eric Nantel - DiaLFonZo
UAV/Drones Specialist

RobotShop inc.
Putting Robotics at your service!™
www.robotshop.com

Lynxmotion
Imagine it. Build it. Control it.™
http://www.Lynxmotion.com


User avatar
Guru ( offline )
Posts: 9256
Posted: 2012-06-14 09:50 
Very large :D

_________________
Jim Frye, the Robot Guy
http://www.lynxmotion.com
I've always tried to do my best...


1, 2

All times are UTC - 5 hours [ DST ]. It is currently 2014-12-19 03:57
Feedback Form
Feedback Form