adding servo code

master
Drew Bednar 4 years ago
parent 80f7ef81d6
commit 7c80611054

@ -31,8 +31,17 @@ cu is a "Call-Up" device meant for calling other devices (e.g. modems). Since we
the dev board we will be using the `/dev/cu.SLAB_USBtoUART` character file for connecting to the board.
### Flashing your board
ESP32
```
esptool.py --chip esp32 -p /dev/cu.SLAB_USBtoUART erase_flash
```
```
esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 115200 write_flash -z 0x1000 esp32spiram-idf3-20200902-v1.13.bin
```
ESP8266
```
esptool.py -p /dev/cu.SLAB_USBtoUART erase_flash
```

@ -79,4 +79,15 @@ def free(full=False):
else : return ('Total:{0} Free:{1} ({2})'.format(T,F,P))
```
esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x1000 ~/Desktop/esp32spiram-idf3-20200902-v1.13.bin
# installing micropython on esp32
1. Delete flash
```
esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash
```
2. If you have external spi ram (4MB) you want to use the spiram bin.
```
esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x1000 ./binaries/esp32-idf4-20210202-v1.14.bin
```

@ -0,0 +1,76 @@
"""Default values provided are from the DS3218MG.
Pins GPIOs 34 to 39 cant generate PWM on the ESP32.
Description:
High-precision metal gears with hard anodizing
CNC aluminum middle Shell
water proof rubber seals screws
Specification:
Stall Torque (5V): 19 kg/cm (263.8oz/in)
Stall Torque (6.8V): 21.5 kg/cm (298.5 oz/in)
Dead band: 3μs
Speed : 0.16 sec/60°(5V) / 0.14 sec/60°(6.8V)
Operating Voltage: 4.8 ~ 6.8 DC Volts
Weight: 60 g (2.12 oz)
Motor Type: DC Motor
Gear Type: Copper & Aluminum
Working frequence: 1520μs / 333hz
CE Certification: Yes
Size: 40 x 20 x 40.5 mm ( 1.58 x 0.79 x 1.60 in)
Package Includes:
4 X DS3218 Digital Servo 20KG
4 X 25T Servo Arm
micro-seconds on 10**-6 seconds.
"""
from machine import Pin, PWM
import math
class Servo:
def __init__(self, pin, freq=40, min_us=500, max_us=2500,
degrees=270):
"""
:param pin: machine.Pin
A PWM enabled Pin
:param freq: int
The frequency in Hz. Typical values for servos are 40 or 50.
:param min_us: int
The minimum number of microseconds
:param max_us:
The maximum number of microseconds
:param degrees: int
The range in degrees of the motor
"""
self.period = 1000000 / freq
self.min_duty = self._us2duty(min_us)
self.max_duty = self._us2duty(max_us)
self.degrees = degrees
self.freq = freq
self.pin = PWM(pin, freq=freq)
def _us2duty(self, value):
"""Converts a microsecond value to a duty cycle value."""
return int(1024 * value / self.period)
def position(self, degrees=None, radians=None, us=None, duty=None):
span = self.max_duty - self.min_duty # 409.6
if degrees is not None:
duty = self.min_duty + span * degrees / self.degrees
elif radians is not None:
duty = self.min_duty + span * radians / math.radians(self.degrees)
elif us is not None:
duty = self._us2duty(us)
elif duty is not None:
pass
else:
return self.pin.duty()
duty = min(self.max_duty, max(self.min_duty, int(duty)))
self.pin.duty(duty)
def release(self):
self.pin.duty(0)

@ -0,0 +1,25 @@
from machine import Pin
from servo import Servo
from time import sleep
p21 = Pin(21)
s21 = Servo(p21, freq=50, min_us=500, max_us=2500,
degrees=270)
while True:
print("Position: 0")
s21.position(degrees=0)
sleep(2)
print("Position: 30")
s21.position(degrees=30)
sleep(2)
print("Position: 60")
s21.position(degrees=60)
sleep(2)
print("Position: 90")
s21.position(degrees=90)
sleep(2)
print("Position: 120")
s21.position(degrees=120)
sleep(2)
Loading…
Cancel
Save