Servo

javadoc

 

The Servo service is used to control servos through a micro-controller such as an Arduino or a Raspberry pi (ServoController).

The service will allow to control attaching or detaching the device, control his position and the speed at wich it move to that position, and turn off/on the servo.

servo.attach(controller, pin, pos, velocity): Initial servo device attachement to the microcontroller

servo.detach(controller): detach the servo from the microcontroller

servo.attach(): Name as arduino servo library, will turn the servo motor on

servo.detach(): will turn the servo motor off

servo.moveTo(pos): ask the servo to move to a position

servo.map(inputMin, inputMax, outputMin, outputMax): default to (0,180, 0, 180). Allow to map a range to a valid output range that can be understand by the servo. The outputMin and outputMax must have value between 0 and 180.

setvo.setInverted(true/false): Reverse the direction of rotation of the servo.

Setup

Connect the signal wire to a pin on the controller

Connect the (+) wire to the (+) of a power source (battery or power supply)

Connect the (-) wire to the ground of a power source AND the ground of the controller

Servo needs a lot of current and require to be connect to a power source that can provide enough current. The controllers (Arduino or Raspberry pi) are not strong enough to make the servo work properly and you should avoid connecting the 5V of the controller to the servo.

 

arduino = Runtime.createAndStart("arduino","Arduino")
arduino.connect("COM15")
 
servo = Runtime.createAndStart("servo","Servo")
servo.attach(arduino,26,90,-1) #attach the servo to pin 26 at an initial position of 90 and without speed control)
#servo.attach(arduino,26,180,20) #attach the servo to pin 26 at an initial position of 180 and will make the servo rotate at the speed of 20 degrees/s)
 
servo.moveTo(0) # move the servo position to 0
 

Speed control (working only when connected to an Arduino)

servo.setVelocity(degree/s): Will force the servo to rotate at the specify velocity. If the speed specify is faster than the servo capability, the servo will turn at a lower speed than expected. The value -1 will bypass the speed control and have the servo rotate normally.

This is a common Servo service.  All simple Servos need to attach to a ServoController.  Currently, the list of ServoControllers are :

The Servo needs a controller in order to operate.  You should be able to select one in the drop down.  Select the correct signal pin the servo is attached to.  Then select "attach" and the servo should be controllable with the slider bar.  Limits can be set in order to protect the servo from going beyon a certain minimum and maximum value.

Now there is an additional method Servo.setSpeed(0.5).  At the moment this is only accessable through Python.  This will set the speed of a servo. The servo will remember it's last speed until it is changed again.  Valid values for speed are between 0.0 and 1.0

Modular Block Diagram showing the differences of sweeping a Servo from MyRobotLab versus sweeping it from the microcontroller (Arduino)

 

Examples:


#file : service/Servo.py edit raw
from org.myrobotlab.service import Arduino
from org.myrobotlab.service import Servo
from org.myrobotlab.service import Runtime

from time import sleep

servoPin = 4
# comPort = "/dev/ttyUSB0"
comPort = "COM15"

# create the services
arduino = Runtime.start("arduino","Arduino")
servo01 = Runtime.start("servo01","Servo")

# initialize arduino
# arduino.connect("/dev/ttyUSB0")
arduino.connect(comPort)

# TODO - set limits
servo01.setMinMax(0, 180)
  
# attach servo
servo01.attach(arduino.getName(), servoPin)

# fast sweep
servo01.moveTo(179)
sleep(0.5)

servo01.moveTo(10)
sleep(0.5)

servo01.moveTo(179)
sleep(0.5)

servo01.moveTo(10)
sleep(0.5)

servo01.moveTo(179)
sleep(0.5)

servo01.moveTo(10)
sleep(0.5)

# speed changes  
servo01.setSpeed(0.99) # set speed to 99% of full speed
servo01.moveTo(90) 
sleep(0.5)

servo01.setSpeed(0.50) # set speed to 50% of full speed
servo01.moveTo(180)
sleep(4)

servo01.setSpeed(1.0) # set speed to 100% of full speed

# moving to rest position
servo01.rest()
sleep(0.5)

# detaching servo
servo01.detach()

MdG_NL's picture

Acceleration option for servo's

Is there a easy way to include a acceleration option in this Servo service or in a service from InMoov ?

Most servo's will start direct in full speed to move to the new position.
This gives a higher risk to burn a servo !

moz4r's picture

Hi Marteen I asked myself the

Hi Marteen I asked myself the same thing :) .

I think we can do increase speed in a thread for smoth gesture ( servo start ) 

but we don't know when the servo reach the position to descelerate. ( the only way is to read maybe the potentiometer value ? )

MdG_NL's picture

Hi Anthony,The main problem

Hi Anthony,

The main problem is the lower resolution, when we only use the 0<>180 degree step resolution in default !

For some functions it would be interesting to use the full microseconds function.

This gives more freedom to play with some settings in acceleration options.

And there is more precision because of the "5 times" higher resolution :)

2400 - 544 = 1856 microseconds

1856 / 180 = 10,3 microseconds per degree

Most servo's have a deadband from 2 microseconds, so this is the minimum stepsize.

10,3 / 2 = 5,15

I don't think we need the feedback from the potentiometers.

We only need to now how many degrees we need to make from position A to B.

If there is a option to use the "look a like" servo setSpeed function like we have in the default servo service, it would be nice.

I'm not that perfect in programming yet and Phyton is new to me...

So I don't now the limitation.