add Leg and Hexapod classes

master
Zhengyu Peng 3 years ago
parent 96617a3e29
commit 44aac11026

@ -5,52 +5,64 @@
# https://circuitpython.readthedocs.io/projects/servokit/en/latest/ # https://circuitpython.readthedocs.io/projects/servokit/en/latest/
from adafruit_servokit import ServoKit from adafruit_servokit import ServoKit
from leg import Leg
import time import time
# Constants
nbPCAServo = 16
class Hexapod:
def __init__(self):
# Objects # Objects
pca1 = ServoKit(channels=16, address=0x40, frequency=120) self.pca_right = ServoKit(channels=16, address=0x40, frequency=120)
pca2 = ServoKit(channels=16, address=0x41, frequency=120) self.pca_left = ServoKit(channels=16, address=0x41, frequency=120)
# function init # front right
self.leg_0 = Leg('fr',
[self.pca_right.servo[15], self.pca_right.servo[1],
def init(): self.pca_right.servo[2]],
print('Init') correction=[0, 0, 0],
constraint=[0, 180])
# for i in range(nbPCAServo): # center right
# pca1.servo[i].set_pulse_width_range(MIN_IMP[i] , MAX_IMP[i]) self.leg_1 = Leg('cr',
# pca2.servo[i].set_pulse_width_range(MIN_IMP[i] , MAX_IMP[i]) [self.pca_right.servo[7], self.pca_right.servo[6],
self.pca_right.servo[8]],
# function main correction=[0, 0, 0],
constraint=[0, 180])
# rear right
self.leg_2 = Leg('rr',
[self.pca_right.servo[0], self.pca_right.servo[13],
self.pca_right.servo[14]],
correction=[0, 0, 0],
constraint=[0, 180])
# rear left
self.leg_3 = Leg('rl',
[self.pca_left.servo[15], self.pca_left.servo[2],
self.pca_left.servo[1]],
correction=[0, 0, 0],
constraint=[0, 180])
# center left
self.leg_4 = Leg('cl',
[self.pca_left.servo[7], self.pca_left.servo[8],
self.pca_left.servo[6]],
correction=[0, 0, 0],
constraint=[0, 180])
# front left
self.leg_5 = Leg('fl',
[self.pca_left.servo[0], self.pca_left.servo[14],
self.pca_left.servo[13]],
correction=[0, 0, 0],
constraint=[0, 180])
self.leg_5.set_angle(0, 45)
self.leg_5.set_angle(1, 45)
self.leg_5.set_angle(2, 45)
def main(): def main():
pcaScenario() hexapod = Hexapod()
# function pcaScenario
def pcaScenario():
"""Scenario to test servo"""
for i in range(nbPCAServo):
pca1.servo[i].angle = 90
pca2.servo[i].angle = 90
# for j in range(MIN_ANG[i], MAX_ANG[i], 1):
# print("Send angle {} to Servo {}".format(j, i))
# pca.servo[i].angle = j
# time.sleep(0.01)
# for j in range(MAX_ANG[i], MIN_ANG[i], -1):
# print("Send angle {} to Servo {}".format(j, i))
# pca.servo[i].angle = j
# time.sleep(0.01)
# pca.servo[i].angle = None # disable channel
time.sleep(0.5)
if __name__ == '__main__': if __name__ == '__main__':
init()
main() main()

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
class Leg:
def __init__(self,
id,
junction_servos,
correction=[0, 0, 0],
constraint=[0, 180]):
self.id = id
self.junction_servos = junction_servos
self.correction = correction
self.constraint = constraint
def set_angle(self, junction, angle):
set_angle = min(angle+self.correction[junction], self.constraint[1])
set_angle = max(set_angle, self.constraint[0])
self.junction_servos[junction].angle = set_angle
Loading…
Cancel
Save