-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmotor_controller.py
76 lines (68 loc) · 2.35 KB
/
motor_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import math
import pins
import sources.motor_controllers as motor_controllers
import sources.motor_drivers as motor_drivers
import RPi.GPIO as GPIO
import wiringpi
#motor setup types
mecanum = 0
differential = 1
omni = 2
#motor driver types
i2c = 0
gpio = 1
class motor_controller:
def __init__(self, logger, d = 20, motor_setup = 0, motor_driver = 1,\
pid = 1, reset = 1, r = 10):
self.logger = logger
GPIO.setmode(GPIO.BCM)
self.GPIO = GPIO
self.motor_controller = motor_setup
self.motor_driver = motor_driver
if motor_driver == gpio:
self.driver = motor_drivers.ada_motor_driver(GPIO, logger)
elif motor_driver == i2c:
self.driver = motor_drivers.i2c_motor_driver(logger)
if motor_setup == mecanum:
self.controller = motor_controllers.mecanum(logger)
if motor_driver == i2c:
self.driver.init_module(pid,reset,4)
elif motor_setup == differential:
self.controller = motor_controllers.differential(d)
if motor_driver == i2c:
self.driver.init_module(pid,reset,2)
#TODO: write motor_setup == omni
#TODO: fix wiringpi
# wiringpi.wiringPiSetup()
# wiringpi.pinMode(1, 2)
''''function move: moves the robot with the given speed, rotation and angle
note that angle is not a valid parameter for differential drive and thus
will not affect the behavior of the robot
@param speed: linear velocity of the robot in m/s (-2 to 2 m/s)
@param rotation: angular velocity of the robot in rad/s
@return: motor values array
'''
def move(self, speed, rotation, angle=0):
motors = self.controller.move(speed,rotation,angle)
for i in range(len(motors)):
self.driver.send_motor_command(i,motors[i])
return motors
def turn(self, speed):
self.driver.turn(speed)
def get_encoders(self):
if self.controller_type != i2c:
self.logger.warning("this controller currently does not support"+\
"encoder feedback")
return self.driver.read_encoders()
def write_rpi_servo(self, pos):
if pos < 0:
self.logger.warning("cannot write negative servo value")
return 0
if pos > 255:
self.logger.warning("servo value too large")
return 0
#TODO: fix wiringpi
#wiringpi.pwmWrite(1,pos)
return 1
def send_servo_command(self, pin, pos):
self.driver.send_servo_command(pin, pos)