Skip to content

Commit c765ef3

Browse files
aykevldeadprogram
authored andcommitted
servo: add driver using PWM
This PR adds support for controlling servos. For example, on the Arduino Uno it should be able to control up to 6 servos jitter-free when using all available PWM pins. I haven't added support for setting a position in degrees, mainly because this varies by servo and it's probably necessary to configure the bounds in some way. Therefore, I added just SetMicroseconds. This makes the API possible to use and leaves the possibility of adding a SetPosition in the future.
1 parent 428db3c commit c765ef3

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ smoke-test:
103103
@md5sum ./build/test.hex
104104
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
105105
@md5sum ./build/test.hex
106+
tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
107+
@md5sum ./build/test.hex
106108
tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
107109
@md5sum ./build/test.hex
108110
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
@@ -196,7 +198,7 @@ DRIVERS = $(wildcard */)
196198
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
197199
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
198200
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x dht keypad4x4 max72xx p1am tone tm1637 \
199-
pcf8563 mcp2515
201+
pcf8563 mcp2515 servo
200202
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
201203

202204
unit-test:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The following 64 devices are supported.
101101
| [PCF8563 real time clock](https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf) | I2C |
102102
| [Resistive Touchscreen (4-wire)](http://ww1.microchip.com/downloads/en/Appnotes/doc8091.pdf) | GPIO |
103103
| [Semihosting](https://wiki.segger.com/Semihosting) | Debug |
104+
| [Servo](https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all) | PWM |
104105
| [Shift register (PISO)](https://en.wikipedia.org/wiki/Shift_register#Parallel-in_serial-out_\(PISO\)) | GPIO |
105106
| [Shift registers (SIPO)](https://en.wikipedia.org/wiki/Shift_register#Serial-in_parallel-out_(SIPO)) | GPIO |
106107
| [SHT3x Digital Humidity Sensor](https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/0_Datasheets/Humidity/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital.pdf) | I2C |

examples/servo/servo.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
"time"
6+
7+
"tinygo.org/x/drivers/servo"
8+
)
9+
10+
// Configuration for the Arduino Uno.
11+
// Please change the PWM and pin if you want to try this example on a different
12+
// board.
13+
var (
14+
pwm = machine.Timer1
15+
pin = machine.D9
16+
)
17+
18+
func main() {
19+
s, err := servo.New(pwm, pin)
20+
if err != nil {
21+
for {
22+
println("could not configure servo")
23+
time.Sleep(time.Second)
24+
}
25+
return
26+
}
27+
28+
println("setting to 0°")
29+
s.SetMicroseconds(1000)
30+
time.Sleep(3 * time.Second)
31+
32+
println("setting to 45°")
33+
s.SetMicroseconds(1500)
34+
time.Sleep(3 * time.Second)
35+
36+
println("setting to 90°")
37+
s.SetMicroseconds(2000)
38+
time.Sleep(3 * time.Second)
39+
40+
for {
41+
time.Sleep(time.Second)
42+
}
43+
}

servo/servo.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package servo
2+
3+
import "machine"
4+
5+
// PWM is the interface necessary for controlling typical servo motors.
6+
type PWM interface {
7+
Configure(config machine.PWMConfig) error
8+
Channel(pin machine.Pin) (channel uint8, err error)
9+
Top() uint32
10+
Set(channel uint8, value uint32)
11+
}
12+
13+
// Array is an array of servos controlled by a single PWM peripheral. On most
14+
// chips, one PWM peripheral can control multiple servos (usually two or four).
15+
type Array struct {
16+
pwm PWM
17+
}
18+
19+
// Servo is a single servo (connected to one PWM output) that's part of a servo
20+
// array.
21+
type Servo struct {
22+
pwm PWM
23+
channel uint8
24+
}
25+
26+
const pwmPeriod = 20e6 // 20ms
27+
28+
// NewArray returns a new servo array based on the given PWM, for if you want to
29+
// control multiple servos from a single PWM peripheral. Using a single PWM for
30+
// multiple servos saves PWM peripherals for other uses and might use less power
31+
// depending on the chip.
32+
//
33+
// If you only want to control a single servo, you could use the New shorthand
34+
// instead.
35+
func NewArray(pwm PWM) (Array, error) {
36+
err := pwm.Configure(machine.PWMConfig{
37+
Period: pwmPeriod,
38+
})
39+
if err != nil {
40+
return Array{}, err
41+
}
42+
return Array{pwm}, nil
43+
}
44+
45+
// Add adds a new servo to the servo array. Please check the chip documentation
46+
// which pins can be controlled by the given PWM: depending on the chip this
47+
// might be rigid (only a single pin) or very flexible (you can pick any pin).
48+
func (array Array) Add(pin machine.Pin) (Servo, error) {
49+
channel, err := array.pwm.Channel(pin)
50+
if err != nil {
51+
return Servo{}, err
52+
}
53+
return Servo{
54+
pwm: array.pwm,
55+
channel: channel,
56+
}, nil
57+
}
58+
59+
// New is a shorthand for NewArray and array.Add. This is useful if you only
60+
// want to control just a single servo.
61+
func New(pwm PWM, pin machine.Pin) (Servo, error) {
62+
array, err := NewArray(pwm)
63+
if err != nil {
64+
return Servo{}, err
65+
}
66+
return array.Add(pin)
67+
}
68+
69+
// SetMicroseconds sets the output signal to be high for the given number of
70+
// microseconds. For many servos the range is normally between 1000µs and 2000µs
71+
// for 90° of rotation (with 1500µs being the 'neutral' middle position).
72+
//
73+
// In many cases they can actually go a bit further, with a wider range of
74+
// supported pulse ranges. For example, they might allow pulse widths from 500µs
75+
// to 2500µs, but be warned that going outside of the 1000µs-2000µs range might
76+
// break the servo as it might destroy the gears if it doesn't support this
77+
// range. Therefore, to be sure check the datasheet before you try values
78+
// outside of the 1000µs-2000µs range.
79+
func (s Servo) SetMicroseconds(microseconds int16) {
80+
value := uint64(s.pwm.Top()) * uint64(microseconds) / (pwmPeriod / 1000)
81+
s.pwm.Set(s.channel, uint32(value))
82+
}

0 commit comments

Comments
 (0)