/
motors.py
146 lines (133 loc) · 4.7 KB
/
motors.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env python
import time
import accel
import string
import sys
SERVO_LEFT = 2.4
SERVO_RIGHT = 0.5
# Connect to I2C bus
i2cHandle = accel.connectI2C(6)
# create handle to magField I2C slave
magField = accel.i2cDev(i2cHandle,0x1e)
# create handle to linAccel I2C slave
linAccel = accel.i2cDev(i2cHandle,0x19)
# create handle to pwm I2C slave
pwm = accel.i2cDev(i2cHandle,0x40)
# Setup Mag and Lin and PWM devices
accel.setupMag(magField)
accel.setupLin(linAccel)
PWM_HZ = accel.setupPwm(pwm,50.0)
def setMotorSpeed(sp,pwm,pwmPair=[0,1]):
if sp == 1:
accel.adjustPwmDuty(pwm,pwmPair[0],0.5,0.0,1)
accel.adjustPwmDuty(pwm,pwmPair[1],0.5,0.0,0)
if sp == -1:
accel.adjustPwmDuty(pwm,pwmPair[0],0.5,0.0,0)
accel.adjustPwmDuty(pwm,pwmPair[1],0.5,0.0,1)
elif sp > 0:
accel.adjustPwmDuty(pwm,pwmPair[0],sp,0.0,2)
accel.adjustPwmDuty(pwm,pwmPair[1],sp,0.0,0)
elif sp < 0:
accel.adjustPwmDuty(pwm,pwmPair[0],abs(sp),0.0,0)
accel.adjustPwmDuty(pwm,pwmPair[1],abs(sp),0.0,2)
else:
accel.adjustPwmDuty(pwm,pwmPair[0],0.5,0.0,1)
accel.adjustPwmDuty(pwm,pwmPair[1],0.5,0.0,1)
def setServoAngle(s,pwm,pwmNum=15):
sp = ((SERVO_LEFT+SERVO_RIGHT)/2) + s*(SERVO_LEFT-SERVO_RIGHT)/2
accel.adjustPwmOnTime(pwm,pwmNum,PWM_HZ,sp,0.4)
def setLightIntensity(sp,pwm,pwmNum=8):
if sp <= 1 and sp >= 0:
accel.adjustPwmDuty(pwm,pwmNum,sp,0.0)
def usage():
print("Enter one of the following commands with associated parameter")
print(" a <speed> // motor a will turn at speed. speed is (-1:1)")
print(" b <speed> // motor b will turn at speed. speed is (-1:1)")
print(" c <speed> // motor a&b will turn at speed. speed is (-1:1)")
print(" s <pos> // servo s will turn to pos. pos is (-1:1)")
print(" t <delay> // servo s will turn right and then left. each step will be delayed by delay")
print(" l <num> <intensity> // light <num (1:2)> on with <intensity (0:1)>")
print(" exit // exit this prog")
s = 0
setServoAngle(s,pwm)
setMotorSpeed(0,pwm,[0,1])
setMotorSpeed(0,pwm,[2,3])
setLightIntensity(0.015,pwm,12)
setLightIntensity(0.015,pwm,13)
if len(sys.argv) > 1:
spl = sys.argv[1:]
if spl[0] == 'a' or spl[0] == 'b' or spl[0] == 'c' or spl[0] == 's' or spl[0] == 't' or spl[0] == 'l':
if len(spl) < 2:
usage()
else:
if len(spl) > 2:
v = float(spl[2])
else:
v = float(spl[1])
if v < -1 or v > 1:
usage()
else:
if spl[0] == 'a' or spl[0] == 'c':
setMotorSpeed(v,pwm,[0,1])
if spl[0] == 'b' or spl[0] == 'c':
setMotorSpeed(v,pwm,[2,3])
if spl[0] == 's':
setServoAngle(v,pwm)
if spl[0] == 't':
for a in range(0,17,1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
for a in range(16,-17,-1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
for a in range(-16,1,1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
if spl[0] == 'l':
setLightIntensity(v,pwm,int(spl[1])+11)
print "%s, %s" % (v, int(spl[1]))
elif spl[0] == 'exit':
run = 0
else:
usage()
else:
run = 1
while run:
raw = raw_input("% ")
if len(raw) == 0:
usage()
else:
spl = string.split(string.lower(raw))
if spl[0] == 'a' or spl[0] == 'b' or spl[0] == 'c' or spl[0] == 's' or spl[0] == 't' or spl[0] == 'l':
if len(spl) < 2:
usage()
else:
if len(spl) > 2:
v = float(spl[2])
else:
v = float(spl[1])
if v < -1 or v > 1:
usage()
else:
if spl[0] == 'a' or spl[0] == 'c':
setMotorSpeed(v,pwm,[0,1])
if spl[0] == 'b' or spl[0] == 'c':
setMotorSpeed(v,pwm,[2,3])
if spl[0] == 's':
setServoAngle(v,pwm)
if spl[0] == 't':
for a in range(0,17,1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
for a in range(16,-17,-1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
for a in range(-16,1,1):
setServoAngle(a*0.01,pwm)
time.sleep(v)
if spl[0] == 'l':
setLightIntensity(v,pwm,int(spl[1])+7)
elif spl[0] == 'exit':
run = 0
else:
usage()