Program for my project

I am glad it is working better for you.

I have not completely studied the thread, so I may have missed a few fine points, but will try to give a few answers/suggestions. Robot Dude will hopefully correct what I missed :smiley:

  1. By default I think these servos are limited to about 140 degrees of rotation. You can use a servo programmer to update them to allow 180 degree rotations. I think you need something like: lynxmotion.com/p-615-hmi-ser … r-kit.aspx to do this.

  2. Not sure what you mean by reprogram my servos when running or how to stop. The current program simply alternates from one position to a second position. You can easily make this more complicated, like only do this N times and stop. That could be done with a simple loop counter and several different basic concepts. You can also change the angle of the sweep by simply changing the Position values (i.e. make them variable instead of a constant). Note: If you stop issuing HSERVO commands, the servo will stay at the last location you specified. It will still be powered and will hold that location. With Analog Servos you can issue an HSERVO command with a special value to completely stop and remove power from the servo, however with digital servos such as yours, I don’t know of any way of doing this.

  3. In this case I am not sure what you mean by RPM as the servo only turns part of 1 revolution. But if I translate your question into: I wish for the sweep from one side to another to take 3 seconds, you can accomplish this by using the speed parameter which is how much to add to the servo value in each servo refresh period, which is every 20ms or 50 times per second. So you might try something like:

MYSERVO con P4 hsPosition var sword Position1 con -7000 'about -45 degrees (adjust to desired angle) Position2 con 7000 'about +45 degrees Time1 con 2000 '2 second pause value (adjust) Time2 con 4000 '4 second pause value HSSpeed con ((Position1-Position2)*20)/3000 ' This calculates the speed value. 3000 is in ms adjust... sound 9, [100\880, 100\988, 100\1046, 100\1175] low 0 HSERVOENABLE ' I don't think this is needed any more hservo[MYSERVO\hsPosition] start: toggle P12 hsPosition = Position1 hservo[MYSERVO\hsPosition\HSSpeed] hservowait[MYSERVO] pause Time1 toggle P13 hsPosition = Position2 hservo[MYSERVO\hsPosition\HSSpeed] hservowait[MYSERVO] pause Time2 toggle p14 goto start

I added a calculation for the speed parameter which hopefully is correct. Also I added the HservoWait commands, which says wait for the servo to complete it’s move.

Hopefully it works…
Kurt

Thank you so much.
My servo was upgraded to 180 degrees before it was shipped to me. I modified you codes as following and it worked for 180 degrees. the problem is that each move was devided into 2 parts: first fast move from 0 to 180 degree and then moves forth a little more like near 10 degrees. Could you tell me what cause that?
In addition, could you please tell me what the ā€œ*20ā€ means in your code ā€œ((Position1-Position2)*20)/3000ā€ ?
The codes I used for the 180 degrees are:

MYSERVO con P4 hsPosition var sword Position1 con -9000 'about -90 degrees (adjust to desired angle) Position2 con 9000 'about +90 degrees Time1 con 5000 '5 second pause value (adjust) Time2 con 5000 '5 second pause value HSSpeed con ((Position1-Position2)*20)/3000 ' This calculates the speed value. 3000 is in ms adjust... sound 9, [100\880, 100\988, 100\1046, 100\1175] low 0 HSERVOENABLE ' I don't think this is needed any more hservo[MYSERVO\hsPosition] start: toggle P12 hsPosition = Position1 hservo[MYSERVO\hsPosition\HSSpeed] hservowait[MYSERVO] pause Time1 toggle P13 hsPosition = Position2 hservo[MYSERVO\hsPosition\HSSpeed] hservowait[MYSERVO] pause Time2 toggle p14 goto start
Thank you again and waiting for your reply.
Yongxin

I guess it would help if I broke out a BAP an tried it :wink:

So I think this is working OK…

MYSERVO con P4 Position1 con -9000 'about -90 degrees (adjust to desired angle) Position2 con 9000 'about +90 degrees Time1 con 5000 '5 second pause value (adjust) Time2 con 5000 '5 second pause value HSSpeed con ((Position2-Position1)*20)/3000 ' This calculates the speed value. 3000 is in ms adjust... sound 9, [100\880, 100\988, 100\1046, 100\1175] low 0 HSERVOENABLE ' I don't think this is needed any more hservo[MYSERVO\0] start: toggle P12 hservo[MYSERVO\Position1\HSSpeed] hservowait[MYSERVO] pause Time1 toggle P13 hservo[MYSERVO\Position2\HSSpeed] hservowait[MYSERVO] pause Time2 toggle p14 goto start
Note: in my debugging I removed the variable to move the new position. I found that I calculated the speed wrong and made it negative instead of positive…

The *20 if because the parameter is how much to add to the value for each servo period of 20ms. So I need to figure out how many periods you have. It would probably be more intuitive if I wrote it as: ā€œ(Position1-Position2)/(3000/20)ā€, which in this case would work fine. But with integer math if you desired time was lets say 2550, it does not evenly divide by 20, so the number may be slightly different so I did it the other way around…

Kurt

It is now running great!:slight_smile: I do appreciate it.
Some more questions:

  1. If I want to change the speed, all I need is just to change 3000 in ((Position2-Position1)*20)/3000, but not ā€œ20ā€?
  2. What codes can I use to tell my servo to have a long pause (eg. 1 hour) after it completes 6 repeads of current ā€œmove forth-pause(5’’)-back-pause(5’’)ā€? could you help me to modify above program for this purpose?
  3. I wish I can have a program which allows me to stop the servo at ether position 1 or position 2 but not between, when I want to stop it. Can you also help me to set up codes for this purpose?
    Thank you again and waiting for your further instruction.
    Yongxin

i must admit, even the simplest bits of code are confusing to me but your explanations are great Kurt.

Where can I get some study materials for me to learn how to program my servo with those codes?
Thanks
Yongxin

You are welcome, I am glad you have things working some for you.

There are lots of books out there that talk about the Basic Language in general. I don’t know of any that are particular to the Basic Atom Pro. In the Help menu there is a selection for Syntax Manual that helps some. You can also look at example programs and the like.

Here is another version of your program, that I used a subroutine to move the servo and wait. It also has a for loop that loops 5 times. Each time through the loop, the sweep times get about a half second longer. It then pauses about 30 seconds, before starting over again…

[code]MYSERVO con P4
i var byte ’ simple variable.
Position1 con -9000 'about -90 degrees (adjust to desired angle)
Position2 con 9000 'about +90 degrees
MoveTime con 500 ’ Take about .5 second
MoveTimeAdj con 500 ’ how much to add per each loop
Time1 con 2000 '2 second pause value (adjust)

Time2 con 2000 '2 second pause value
HSSpeed con ((Position2-Position1)*20)/3000 ’ This calculates the speed value. 3000 is in ms adjust…
sound 9, [100\880, 100\988, 100\1046, 100\1175]

hservo[MYSERVO\0] ’ Make sure the servo has an initial position…
start:
for i = 0 to 4 ’ lets loop 5 times (0 biases)
gosub MoveServo[MYSERVO, Position1, MoveTime + MoveTimeAdji] ’ function is defined to take servo number, position and time in ms
pause Time1
gosub MoveServo[MYSERVO, Position2, MoveTime + MoveTimeAdj
i]
pause Time2
next

sound 9, [100\880] 
pause 30000	' Pause 30 seconds
sound 9, [100\1175] 
goto start 

_MS_Servo var byte
_MS_Pos var sword
_MS_Time var long
MoveServo[_MS_Servo, _MS_Pos, _MS_Time]:
HServo [_MS_Servo_MS_Pos(ABS(_MS_Pos - (HServoPos _MS_Servo))*20)/_MS_Time]
HServoWait[_MS_Servo]
return[/code]

I know this does not do what specific things you wanted, but hopefully there is enough here to give you some ideas on how you might accomplish what you need.

Good luck

Kurt

Thank you very much, Kurt.
I learnt a lot from your codes, though I do not understand many of them.
I am going to try some tomorrow morning and then very possibly bother you again :wink: .
Yongxin

hi, Kurt,
your codes are almost perfect for my purpose except that the servo travels slower and slower in each loop, and after the longer pause (Pause 3000), it restarts the process from fast to slow. Can you help me to remove those codes that change the servo travel speed in each loop? I do not know which are for it and I tried several times and always made some ā€œerrorsā€.
Thank you.
Yongxin

Change this…

for i = 0 to 4 ' lets loop 5 times (0 biases) gosub MoveServo[MYSERVO, Position1, MoveTime + MoveTimeAdj*i] ' function is defined to take servo number, position and time in ms pause Time1 gosub MoveServo[MYSERVO, Position2, MoveTime + MoveTimeAdj*i] pause Time2 next

To this…

for i = 0 to 4 ' lets loop 5 times (0 biases) gosub MoveServo[MYSERVO, Position1, MoveTime] ' function is defined to take servo number, position and time in ms pause Time1 gosub MoveServo[MYSERVO, Position2, MoveTime] pause Time2 next

It would be so much easier if you just list in very specific terms what you want it to do and I or Kurt will try to make the program.

Hi, Jim
It worked so well after the change. I am really happy with it and very grateful for your help.
I also wish to know how to use those ā€œsleepā€ codes that kurt wrote me before. Just simply put them at the end or it needs some special format?
The codes are:
; simple function to sleep for about a day.
_iSADCount var long
SleepADay:
for _iSADCount = 0 to 60*24 ; number of minutes in a day.
sleep 60000 ; sleep the number of milliseconds per minute…
next
return
Thank you.
Yongxin

Try this… It should do the same behavior as it does now, but after one pass it waits for 24 hours before it runs it again. It should beep once every minute when running the delay. (if I did it right) lol Note this will not be exactly 24 hours without some fine tuning…

[code]MYSERVO con P4
i var byte ’ simple variable.
Position1 con -9000 'about -90 degrees (adjust to desired angle)
Position2 con 9000 'about +90 degrees
MoveTime con 500 ’ Take about .5 second
MoveTimeAdj con 500 ’ how much to add per each loop
Time1 con 2000 '2 second pause value (adjust)

Time2 con 2000 '2 second pause value
HSSpeed con ((Position2-Position1)*20)/3000 ’ This calculates the speed value. 3000 is in ms adjust…
sound 9, [100\880, 100\988, 100\1046, 100\1175]

hservo[MYSERVO\0] ’ Make sure the servo has an initial position…
start:
for i = 0 to 4 ’ lets loop 5 times (0 biases)
gosub MoveServo[MYSERVO, Position1, MoveTime + MoveTimeAdji] ’ function is defined to take servo number, position and time in ms
pause Time1
gosub MoveServo[MYSERVO, Position2, MoveTime + MoveTimeAdj
i]
pause Time2
next

sound 9, [100\880]
pause 30000 ’ Pause 30 seconds
sound 9, [100\1175]
gosub SleepADay
goto start

_MS_Servo var byte
_MS_Pos var sword
_MS_Time var long
MoveServo[_MS_Servo, _MS_Pos, _MS_Time]:
HServo [_MS_Servo_MS_Pos(ABS(_MS_Pos - (HServoPos _MS_Servo))*20)/_MS_Time]
HServoWait[_MS_Servo]
return

_iSADCount var long
SleepADay:
for _iSADCount = 0 to 60*24 ; number of minutes in a day.
sleep 60000 ; sleep the number of milliseconds per minute…
sound 9, [100\880]
next
return[/code]

Thank you so much and I am going to test it again.

Hi, Jim,
I tried the last program with the ā€œsleepā€ codes. I does not really make my servo sleep, instead, it make the servo frequently vibrate about once per 3 seconds (beeps). Can you help me to fix that by modifying the codes?
thank you.
Yongxin

The servo will never turn off because it is digital.

A analog servo requires power and signal pulses to hold position.

A digital servo only requires power to hold position. It will hold position with or without signal pulses.

If you use an analog serrvo like the HS-485 or HS-645 we can turn the pulses off during the long pauses. Do you have an analog servo?

No, I do not have analog serrvo, but I can order one if it can meet all of my requirements. Can analog serrvo be programmed as for digital servo? and we can still use Bot Board II to program it? After we turn the pulse off, can it automatically restart at the time we set?

When we use Bot Board II, for either digital or analog serrvo, do we have a way to start the program through computer instead of connect or disconnect the power of the servo?

Thanks.
Yongxin

The programmer is only required for digital servos. Analog servos already have the full 180 degrees of rotation out of the box. It will be able to be turned off and restarted after the time you set. I’m not sure how to have the PC turn it on and off though.

For analog servo, do we have any way to adjust its rotation speed and frequency as well as the pause time between its each move?

Yes it will be possible to control all of these parameters. No problem.

Great! thank you.
Can you tell me which items I should order? I need relatively more powerful servo, such as the torque >300 oz.
Thanks again.
yongxin