forked from CoderBotOrg/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoderbot.py
More file actions
228 lines (188 loc) · 7.32 KB
/
coderbot.py
File metadata and controls
228 lines (188 loc) · 7.32 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
############################################################################
# CoderBot, a didactical programmable robot.
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
############################################################################
import os
import time
import pigpio
import config
import logging
import sonar
PIN_MOTOR_ENABLE = 22
PIN_LEFT_FORWARD = 25
PIN_LEFT_BACKWARD = 24
PIN_RIGHT_FORWARD = 4
PIN_RIGHT_BACKWARD = 17
PIN_PUSHBUTTON = 11
PIN_SERVO_3 = 9
PIN_SERVO_4 = 10
PIN_SONAR_1_TRIGGER = 18
PIN_SONAR_1_ECHO = 7
PIN_SONAR_2_TRIGGER = 18
PIN_SONAR_2_ECHO = 8
PIN_SONAR_3_TRIGGER = 18
PIN_SONAR_3_ECHO = 23
PIN_ENCODER_LEFT = 14
PIN_ENCODER_RIGHT = 15
PWM_FREQUENCY = 100 #Hz
PWM_RANGE = 100 #0-100
def coderbot_callback(gpio, level, tick):
return CoderBot.get_instance().callback(gpio, level, tick)
class CoderBot:
_pin_out = [PIN_MOTOR_ENABLE, PIN_LEFT_FORWARD, PIN_RIGHT_FORWARD, PIN_LEFT_BACKWARD, PIN_RIGHT_BACKWARD, PIN_SERVO_3, PIN_SERVO_4]
def __init__(self, servo=False, motor_trim_factor=1.0):
self.pi = pigpio.pi('localhost')
self.pi.set_mode(PIN_PUSHBUTTON, pigpio.INPUT)
self.pi.set_mode(PIN_ENCODER_LEFT, pigpio.INPUT)
self.pi.set_mode(PIN_ENCODER_RIGHT, pigpio.INPUT)
self._cb = dict()
self._cb_last_tick = dict()
self._cb_elapse = dict()
self._servo = servo
self._motor_trim_factor = motor_trim_factor
if self._servo:
self.motor_control = self._servo_motor
else:
self.motor_control = self._dc_motor
self._cb1 = self.pi.callback(PIN_PUSHBUTTON, pigpio.EITHER_EDGE, coderbot_callback)
self._cb2 = self.pi.callback(PIN_ENCODER_LEFT, pigpio.RISING_EDGE, coderbot_callback)
self._cb3 = self.pi.callback(PIN_ENCODER_RIGHT, pigpio.RISING_EDGE, coderbot_callback)
for pin in self._pin_out:
self.pi.set_PWM_frequency(pin, PWM_FREQUENCY)
self.pi.set_PWM_range(pin, PWM_RANGE)
self.stop()
self._is_moving = False
self.sonar = [sonar.Sonar(self.pi, PIN_SONAR_1_TRIGGER, PIN_SONAR_1_ECHO),
sonar.Sonar(self.pi, PIN_SONAR_2_TRIGGER, PIN_SONAR_2_ECHO),
sonar.Sonar(self.pi, PIN_SONAR_3_TRIGGER, PIN_SONAR_3_ECHO)]
self._encoder_cur_left = 0
self._encoder_cur_right = 0
self._encoder_target_left = -1
self._encoder_target_right = -1
the_bot = None
def exit(self):
self._cb1.cancel()
self._cb2.cancel()
self._cb3.cancel()
for s in self.sonar:
s.cancel()
@classmethod
def get_instance(cls, servo=False, motor_trim_factor=1.0):
if not cls.the_bot:
cls.the_bot = CoderBot(servo, motor_trim_factor)
return cls.the_bot
def move(self, speed=100, elapse=-1):
self.motor_control(speed_left=min(100, max(-100, speed * self._motor_trim_factor)), speed_right=min(100, max(-100, speed / self._motor_trim_factor)), elapse=elapse)
def turn(self, speed=100, elapse=-1):
self.motor_control(speed_left=min(100, max(-100, speed / self._motor_trim_factor)), speed_right=-min(100, max(-100, speed * self._motor_trim_factor)), elapse=elapse)
def forward(self, speed=100, elapse=-1):
self.move(speed=speed, elapse=elapse)
def backward(self, speed=100, elapse=-1):
self.move(speed=-speed, elapse=elapse)
def left(self, speed=100, elapse=-1):
self.turn(speed=-speed, elapse=elapse)
def right(self, speed=100, elapse=-1):
self.turn(speed=speed, elapse=elapse)
def servo3(self, angle):
self._servo_control(PIN_SERVO_3, angle)
def servo4(self, angle):
self._servo_control(PIN_SERVO_4, angle)
def get_sonar_distance(self, sonar_id=0):
return self.sonar[sonar_id].get_distance()
def _dc_motor(self, speed_left=100, speed_right=100, elapse=-1, steps_left=-1, steps_right=-1 ):
self._encoder_cur_left = 0
self._encoder_cur_right = 0
self._encoder_target_left = steps_left
self._encoder_target_right = steps_right
self._is_moving = True
if speed_left < 0:
speed_left = abs(speed_left)
self.pi.write(PIN_LEFT_FORWARD, 0)
self.pi.set_PWM_dutycycle(PIN_LEFT_BACKWARD, speed_left)
else:
self.pi.write(PIN_LEFT_BACKWARD, 0)
self.pi.set_PWM_dutycycle(PIN_LEFT_FORWARD, speed_left)
if speed_right < 0:
speed_right = abs(speed_right)
self.pi.write(PIN_RIGHT_FORWARD, 0)
self.pi.set_PWM_dutycycle(PIN_RIGHT_BACKWARD, speed_right)
else:
self.pi.write(PIN_RIGHT_BACKWARD, 0)
self.pi.set_PWM_dutycycle(PIN_RIGHT_FORWARD, speed_right)
self.pi.write(PIN_MOTOR_ENABLE, 1)
if elapse > 0:
time.sleep(elapse)
self.stop()
def _servo_motor(self, speed_left=100, speed_right=100, elapse=-1):
self._is_moving = True
speed_left = -speed_left
self.pi.write(PIN_MOTOR_ENABLE, 1)
self.pi.write(PIN_RIGHT_BACKWARD, 0)
self.pi.write(PIN_LEFT_BACKWARD, 0)
self._servo_motor_control(PIN_LEFT_FORWARD, speed_left)
self._servo_motor_control(PIN_RIGHT_FORWARD, speed_right)
if elapse > 0:
time.sleep(elapse)
self.stop()
def _servo_motor_control(self, pin, speed):
self._is_moving = True
speed = ((speed + 100) * 50 / 200) + 52
self.pi.set_PWM_range(pin, 1000)
self.pi.set_PWM_frequency(pin, 50)
self.pi.set_PWM_dutycycle(pin, speed)
def _servo_control(self, pin, angle):
duty = ((angle + 90) * 100 / 180) + 25
self.pi.set_PWM_range(pin, 1000)
self.pi.set_PWM_frequency(pin, 50)
self.pi.set_PWM_dutycycle(pin, duty)
def stop(self):
for pin in self._pin_out:
self.pi.write(pin, 0)
self._is_moving = False
def is_moving(self):
return self._is_moving
def set_callback(self, gpio, callback, elapse):
self._cb_elapse[gpio] = elapse * 1000
self._cb[gpio] = callback
self._cb_last_tick[gpio] = 0
def callback(self, gpio, level, tick):
if gpio == PIN_ENCODER_LEFT:
self._encoder_cur_left += 1
if self._encoder_target_left >= self._encoder_cur_left:
self.pi.write(PIN_LEFT_FORWARD, 0)
self.pi.write(PIN_LEFT_BACKWARD, 0)
elif gpio == PIN_ENCODER_RIGHT:
self._encoder_cur_right += 1
if self._encoder_target_right >= self._encoder_cur_right:
self.pi.write(PIN_RIGHT_FORWARD, 0)
self.pi.write(PIN_RIGHT_BACKWARD, 0)
else:
cb = self._cb.get(gpio)
if cb:
elapse = self._cb_elapse.get(gpio)
if level == 0:
self._cb_last_tick[gpio] = tick
elif tick - self._cb_last_tick[gpio] > elapse:
self._cb_last_tick[gpio] = tick
print "pushed: ", level, tick
cb()
def halt(self):
os.system ('sudo halt')
def restart(self):
os.system ('sudo /etc/init.d/coderbot restart')
def reboot(self):
os.system ('sudo reboot')