servo-server is a simple yet useful tool to control pan-tilt servos through a socket interface. Thus, it hides all the complexity required to program a servo controller directly.
More precisely, when it starts, it automatically detects and configures a supported servo controller when it is plugged into the system and waits for the user commands by listening to a given UNIX Domain Socket (i.e. a special file). Then moving the servos to the desired positions is as easy as writing the following JSON string to the socket: {"set": {"pan_angle": <pan_angle>, "tilt_angle": <tilt_angle>}}
(where <pan_angle> and <tilt_angle> are the position angles in degrees). You can optionally specify one angle only.
The following servo controllers are supported:
- Micro Maestro USB Servo Controller – The 6-channel model over USB + HS-422 servos have been tested, but other Maestro models should work too. USB Plug & play is supported.
The six-channel Micro Maestro raises the performance bar for serial servo controllers with features such as a native USB interface and internal scripting control. Whether you want high-performance servo control (0.25 μs resolution with built-in speed and acceleration control) .
the manufacturer
Installation for ARM (e.g. Raspberry Pi)
If you have never installed other software present on this site before and are running Raspbian Stretch or Raspbian Buster (also known as Raspberry PI OS) or possibly any future releases, type the following commands:
$ curl https://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add - $ echo "deb https://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main" | sudo tee /etc/apt/sources.list.d/uv4l.list
Update the system and install the package:
$ sudo apt update $ sudo apt install servo-server
Once installed, you can run the installed servo_server binary (note the ‘_’ character in the name) to get the list of the command line options available:
$ servo_server --help Data channel options: --socket arg (=/tmp/uv4l.socket) path to the unix domain socket -i [ --ignore-commands ] [=arg(=1)] (=0) do not actuate any commands received through the data channel --restore-home [=arg(=1)] (=1) send commands to servos to restore their home positions when the data channel session is created -v [ --verbosity ] [=arg(=1)] (=0) 0=print warning or error messages to stdout, 1=print data received from the data channel to stdout --echo [=arg(=1)] (=0) echo any received messages back through the data channel Pan/Tilt options (defaults are tailored to the HS422 servos): --pan-channel arg (=0) pan channel --pan-min-us arg (=600) pan min pulse width in microseconds --pan-max-us arg (=2400) pan max pulse width in microseconds --pan-neutral-us arg (=1500) pan neutral pulse width in microseconds --pan-home-mode arg (=goto) set home mode: 0=off, 1=ignore, 2=go to home on error and power on --pan-home-us arg (=1500) pan home in microseconds --pan-speed arg (=0) pan speed --pan-acceleration arg (=0) pan acceleration --pan-min-angle arg (=0) logical pan min angle corresponding to min pulse width --pan-max-angle arg (=180) logical pan max angle corresponding to max pulse width --pan-go-home [=arg(=1)] (=1) go to home position when the pan servo is detected --tilt-channel arg (=1) tilt channel --tilt-min-us arg (=600) tilt min pulse width in microseconds --tilt-max-us arg (=2400) tilt max pulse width in microseconds --tilt-neutral-us arg (=1500) tilt neutral pulse width in microseconds --tilt-home-mode arg (=goto) set home mode: 0=off, 1=ignore, 2=go to home on error and power on --tilt-home-us arg (=1500) tilt home in microseconds --tilt-speed arg (=0) tilt speed --tilt-acceleration arg (=0) tilt acceleration --tilt-min-angle arg (=0) logical tilt min angle corresponding to min pulse width --tilt-max-angle arg (=180) logical tilt max angle corresponding to max pulse width --tilt-go-home [=arg(=1)] (=1) go to home position when the tilt servo is detected Other options: --config-file arg path to the configuration file specifying option values in the form <option>=<value>. Options specified via command line have higher priority. Default is /etc/servo_server.conf if present. -h [ --help ] print help and exit
Usage example
Run servo_server and plug the servo controller into the system or viceversa ( the order does not matter):
$ servo_server /tmp/servo.socket
Below is an example of a client written in python. Once executed, it waits for the up, down, left, right arrow key presses from the user to send the corresponding commands to the server (via socket) in order to orientate the pan/tilt servos. The most updated version of the code shown below is available in /usr/share/servo_server/servo_client.py. This example depends on the keyboard python package which in turn requires the script to be run as root to detect the key presses (‘q’ to quit):
$ sudo /usr/share/servo_server/servo_client.py /tmp/servo.socket
The source code of the python program is reported below.
#!/usr/bin/python3 import socket import time import os from termios import tcflush, TCIOFLUSH import sys import keyboard socket_path = sys.argv[1] if len(sys.argv) >= 2 else '/tmp/servo.socket' if not(os.path.exists(socket_path)): print(socket_path, 'does not exist!', file=sys.stderr) exit(1) s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) print('socket_path:', socket_path) s.connect(socket_path) def move_servos(pan_angle, tilt_angle): data = '{{"set": {{"pan_angle": {}, "tilt_angle": {}}}}}'.format(pan_angle, tilt_angle) #print(data) s.send(data.encode()) pan_angle, tilt_angle = 90, 90 # home move_servos(pan_angle, tilt_angle); print("Press Up, Down, Left, Right arrow keys to move the servos, 'q' to exit") while True: key = keyboard.read_key() if key == 'q': print('Bye bye!') break elif key == 'left': pan_angle = min(pan_angle + 1, 180) elif key == 'right': pan_angle = max(pan_angle - 1, 0) elif key == 'down': tilt_angle = max(tilt_angle - 1, 0) elif key == 'up': tilt_angle = min(tilt_angle + 1, 180) if key in ['left', 'right', 'up', 'down']: move_servos(pan_angle, tilt_angle) time.sleep(0.01) tcflush(sys.stdin, TCIOFLUSH)
Support for other servo controllers
Feel free to Contact Us.