1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 'Use for motor control'
16
17 import time
18
19 PORT_A = 0x00
20 PORT_B = 0x01
21 PORT_C = 0x02
22 PORT_ALL = 0xFF
23
24 MODE_IDLE = 0x00
25 MODE_MOTOR_ON = 0x01
26 MODE_BRAKE = 0x02
27 MODE_REGULATED = 0x04
28
29 REGULATION_IDLE = 0x00
30 REGULATION_MOTOR_SPEED = 0x01
31 REGULATION_MOTOR_SYNC = 0x02
32
33 RUN_STATE_IDLE = 0x00
34 RUN_STATE_RAMP_UP = 0x10
35 RUN_STATE_RUNNING = 0x20
36 RUN_STATE_RAMP_DOWN = 0x40
37
38 LIMIT_RUN_FOREVER = 0
39
41
55
57 if self.debug:
58 print message
59
61 self._debug_out('Setting brick output state...')
62 self.brick.set_output_state(self.port, self.power, self.mode, self.regulation, self.turn_ratio, self.run_state, self.tacho_limit)
63 self._debug_out('State set.')
64
66 self._debug_out('Getting brick output state...')
67 values = self.brick.get_output_state(self.port)
68 (self.port, self.power, self.mode, self.regulation,
69 self.turn_ratio, self.run_state, self.tacho_limit,
70 self.tacho_count, self.block_tacho_count,
71 self.rotation_count) = values
72 self._debug_out('State got.')
73 return values
74
77
78 - def run(self, power=100, regulated=1):
91
92 - def stop(self, braking=1):
106
107 - def update(self, power, tacho_limit, braking=False, max_retries=-1):
108 '''Use this to run a motor. power is a value between -127 and 128, tacho_limit is
109 the number of degrees to apply power for. Braking is wether or not to stop the
110 motor after turning the specified degrees (unreliable). max_retries is the
111 maximum times an internal loop of the braking function runs, so it doesn't get
112 caught in an infinite loop (deprecated, do not use).'''
113 if max_retries != -1:
114 print 'Warning: max_retries is deprecated and is not longer needed, please do not use it!'
115
116 if braking:
117 direction = (power > 0)*2-1
118 if abs(power) < 50:
119 power = 50 * direction
120 self.get_output_state()
121 starting_tacho_count = self.tacho_count
122 self._debug_out('tachocount: '+str(starting_tacho_count))
123 tacho_target = starting_tacho_count + tacho_limit*direction
124 self._debug_out('tacho target: '+str(tacho_target))
125
126
127 self.mode = MODE_MOTOR_ON
128 self.regulation = REGULATION_IDLE
129 self.turn_ratio = 0
130 self.run_state = RUN_STATE_RUNNING
131 self.power = power
132 self.tacho_limit = tacho_limit
133 self._debug_out('Updating motor information...')
134
135 if braking:
136 self.run(power)
137 current_tacho = 0
138 retries = 0
139 delta = abs(power)*0.5
140
141 while 1:
142 self._debug_out('checking tachocount...')
143 self.get_output_state()
144 current_tacho = self.tacho_count
145 self._debug_out('tachocount: '+str(current_tacho))
146
147 if ((power >= 0 and (current_tacho >= tacho_target - delta)) or
148 (power < 0 and (current_tacho <= tacho_target + delta))):
149 self._debug_out('tachocount good, breaking from loop...')
150 break
151 else:
152 self._debug_out('tachocount bad, trying again...')
153
154 self.stop(1)
155 self._debug_out('difference from goal: '+str(self.get_output_state()[7]-tacho_target))
156
157 else:
158 self.set_output_state()
159 self._debug_out('Updating finished. ending tachocount: '+str(self.get_output_state()[7]))
160