Skip to content

Commit 0dbee27

Browse files
author
Thomas Preston
committed
Added servo support
2 parents fba5bd7 + 25f8694 commit 0dbee27

File tree

8 files changed

+106
-9
lines changed

8 files changed

+106
-9
lines changed

CHANGELOG

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Change Log
22
==========
33

4+
v0.7.4
5+
------
6+
- Fixed a bug with io_pwm() running three times faster than it should.
7+
Fixes #15.
8+
- Added servo support.
9+
10+
v0.7.3
11+
------
12+
- Fixed a bug with colourtail timing issue in the firmware.
13+
414
v0.7.2
515
------
616
- Fixed a bug with colourtail using the new set_bulk.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ Features:
1010
- Inputs/Outputs (legs, buttons, extension pins)
1111
- Input/Output configuration (direction and pullups)
1212
- Extension pin control (I/O, SPI, I2C)
13+
14+
15+
Updating Notes
16+
--------------
17+
Be sure to update the version number in:
18+
19+
codebug_tether/version.py
20+
CHANGELOG
21+
docs/conf.py

codebug_tether/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
IO_PWM_OUTPUT,
66
T2_PS_1_1,
77
T2_PS_1_4,
8-
T2_PS_1_16)
8+
T2_PS_1_16,
9+
scale)

codebug_tether/core.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def pwm_on(self, t2_prescale, full_period, on_period):
172172
"""Turns on the PWM generator with the given settings.
173173
174174
:param t2_prescale: One of T2_PS_1_1, T2_PS_1_4, T2_PS_1_16
175-
Scales down the 4MHz instruction clock by
175+
Scales down the 12MHz instruction clock by
176176
1, 4 or 16.
177177
:param full_period: 8-bit value - which is scaled up to 10-bits
178178
(<< 2) - to which timer 2 will count up to
@@ -182,7 +182,7 @@ def pwm_on(self, t2_prescale, full_period, on_period):
182182
full_period to control duty cycle. For
183183
example:
184184
185-
# 4MHz / 16 with 50% duty cycle
185+
# 12MHz / 16 with 50% duty cycle
186186
codebug.pwm_on(T2_PS_1_16, 0xff, 0x200)
187187
188188
"""
@@ -205,14 +205,13 @@ def pwm_freq(self, frequency):
205205
206206
"""
207207
# calculate pwm settings
208-
# 4 MHz / 16 = 250k ticks per second
209-
full_period = int(250000 / frequency) - 1
208+
# 12MHz / 16 = 750k ticks per second
209+
full_period = int(750000 / frequency) - 1
210210
# for 50% duty cycle: shift up by 2 then /(2 i.e. 50% duty cycle)
211211
# on_period = (full_period << 2) / 2;
212212
# this is quicker
213213
on_period = full_period << 1
214214
self.pwm_on(T2_PS_1_16, full_period, on_period)
215-
pass
216215

217216
def pwm_off(self):
218217
"""Turns off the PWM generator."""
@@ -559,3 +558,19 @@ def uart_rx_is_ready(self):
559558
def uart_rx_get_buffer(self, length, offset=0):
560559
"""Returns data bytes from UART buffer."""
561560
return self.get_buffer(UART_RX_BUFFER_INDEX, length, offset)
561+
562+
563+
def scale(x, from_low, from_high, to_low, to_high):
564+
# Hardware can only do 16bit maths
565+
def limit(v):
566+
max_value = 0x7fff
567+
min_value = -0x8000
568+
return max(min_value, min(max_value, v))
569+
x, from_low, from_high, to_low, to_high = map(
570+
limit, (x, from_low, from_high, to_low, to_high))
571+
# do the scale
572+
from_delta = from_high - from_low
573+
x_offset = x - from_low
574+
to_delta = to_high - to_low
575+
new_x = int((x_offset * to_delta) / from_delta)
576+
return to_low + new_x

codebug_tether/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.7.2'
1+
__version__ = '0.7.4'

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
#
5151
# The short X.Y version.
5252
# The short X.Y version.
53-
version = '0.6.0'
53+
version = '0.7.3'
5454
# The full version, including alpha/beta/rc tags.
55-
release = '0.6.0'
55+
release = '0.7.3'
5656

5757
# The language for content autogenerated by Sphinx. Refer to documentation
5858
# for a list of supported languages.

docs/example.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,68 @@ Or you can be more specific with the duty cycle and timing::
160160
>>> codebug.pwm_off()
161161

162162

163+
Servos
164+
======
165+
It is possible to drive up to eight servos from CodeBug. Servos
166+
typically operate by sending them a PWM (Pulse Width Modulation) signal
167+
with a 20ms period and a 1-2ms duty cycle which controls the rotation
168+
angle of the servo. For example::
169+
170+
Pulse Length
171+
<-1-2ms->
172+
1+---------+
173+
| |
174+
0+ +--------------------------------------+
175+
<------------------20ms (50Hz)----------------->
176+
PWM Period
177+
178+
A duty cycle of 1ms might correspond to 0° rotation and a duty cycle of
179+
2ms might correspond to 180° rotation. Although the precise values may
180+
differ depending on the type of servo.
181+
182+
In order to drive servos from CodeBug you can call the `servo_set()`
183+
method which takes the servo index (which leg you are driving the
184+
servo from) and the the pulse length specified in N 0.5μs. For example::
185+
186+
>>> import codebug_tether
187+
>>> from codebug_tether import (IO_DIGITAL_OUTPUT, scale)
188+
189+
>>> # init CodeBug and configure leg 0 to be digital output
190+
>>> codebug = codebug_tether.CodeBug()
191+
>>> codebug.set_leg_io(0, IO_DIGITAL_OUTPUT)
192+
193+
>>> # set servo on leg 0 with pulse length of 1ms (2000 * 0.5μs)
194+
>>> codebug.servo_set(0, 2000)
195+
196+
>>> # stop driving the servo on leg 0
197+
>>> codebug.servo_set(0, 0)
198+
199+
You can use the scale function to easily calculate the required pulse
200+
length value like so::
201+
202+
>>> import codebug_tether
203+
>>> from codebug_tether import (IO_DIGITAL_OUTPUT, scale)
204+
205+
>>> # scale 50 in the range 0-100 to the range 0-255
206+
>>> scale(50, 0, 100, 0, 255)
207+
127
208+
209+
>>> # scale 10 in the range 0-30 to the range 100-400
210+
>>> scale(10, 0, 30, 100, 400)
211+
200
212+
213+
>>> # scale 90° in the range 0-180° to the range 2000-4000 * 0.5μs
214+
>>> scale(90, 0, 180, 2000, 4000)
215+
3000
216+
217+
>>> # init CodeBug and configure leg 0 to be digital output
218+
>>> codebug = codebug_tether.CodeBug()
219+
>>> codebug.set_leg_io(0, IO_DIGITAL_OUTPUT)
220+
221+
>>> # drive the servo to be at 90 degrees
222+
>>> codebug.servo_set(0, scale(90, 0, 180, 2000, 4000))
223+
224+
163225
Colour Tail
164226
===========
165227
You can control Colour Tails (WS2812's) attached to CodeBug. By default,

firmware/codebug_tether_v0.7.3.cbg

17.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)