Skip to content

Commit 59d685f

Browse files
committed
Fixed bug on distance control movement, extended routes with new distance control movement, new test cases
1 parent ba80ac3 commit 59d685f

File tree

7 files changed

+89
-35
lines changed

7 files changed

+89
-35
lines changed

api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ def stop():
106106
return 200
107107

108108
def move(data):
109-
bot.move(speed=data["speed"], elapse=data["elapse"])
109+
bot.move(speed=data["speed"], time_elapse=data["elapse"], target_distance=data["distance"])
110110
return 200
111111

112112
def turn(data):
113-
bot.turn(speed=data["speed"], elapse=data["elapse"])
113+
bot.turn(speed=data["speed"], time_elapse=data["elapse"])
114114
return 200
115115

116116
def exec(data):

coderbot.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,36 @@ def get_instance(cls, servo=False, motor_trim_factor=1.0):
114114
cls.the_bot = CoderBot(servo, motor_trim_factor)
115115
return cls.the_bot
116116

117-
def move(self, speed=100, time_elapse=0, target_distance=0):
117+
def move(self, speed=100, elapse=0, distance=0):
118+
self._motor_trim_factor = 1.0
118119
speed_left = min(100, max(-100, speed * self._motor_trim_factor))
119120
speed_right = min(100, max(-100, speed / self._motor_trim_factor))
120-
self.motor_control(speed_left=speed_left, speed_right=speed_right, time_elapse=time_elapse, target_distance=target_distance)
121+
self.motor_control(speed_left=speed_left, speed_right=speed_right, time_elapse=elapse, target_distance=distance)
121122

122-
def turn(self, speed=100, time_elapse=0):
123+
def turn(self, speed=100, elapse=0):
123124
speed_left = min(100, max(-100, speed * self._motor_trim_factor))
124125
speed_right = -min(100, max(-100, speed / self._motor_trim_factor))
125-
self.motor_control(speed_left=speed_left, speed_right=speed_right, time_elapse=time_elapse)
126+
self.motor_control(speed_left=speed_left, speed_right=speed_right, time_elapse=elapse)
126127

127128
def turn_angle(self, speed=100, angle=0):
128129
z = self._ag.get_gyro_data()['z']
129-
self.turn(speed, time_elapse=0)
130+
self.turn(speed, elapse=0)
130131
while abs(z - self._ag.get_gyro_data()['z']) < angle:
131132
time.sleep(0.05)
132133
logging.info(self._ag.get_gyro_data()['z'])
133134
self.stop()
134135

135-
def forward(self, speed=100, time_elapse=0):
136-
self.move(speed=speed, time_elapse=time_elapse)
136+
def forward(self, speed=100, elapse=0, distance=0):
137+
self.move(speed=speed, elapse=elapse, distance=distance)
137138

138-
def backward(self, speed=100, time_elapse=0):
139-
self.move(speed=-speed, time_elapse=elapse)
139+
def backward(self, speed=100, elapse=0, distance=0):
140+
self.move(speed=-speed, elapse=elapse, distance=distance)
140141

141-
def left(self, speed=100, time_elapse=0):
142-
self.turn(speed=-speed, time_elapse=time_elapse)
142+
def left(self, speed=100, elapse=0):
143+
self.turn(speed=-speed, elapse=elapse)
143144

144-
def right(self, speed=100, time_elapse=0):
145-
self.turn(speed=speed, time_elapse=time_elapse)
145+
def right(self, speed=100, elapse=0):
146+
self.turn(speed=speed, elapse=elapse)
146147

147148
def get_sonar_distance(self, sonar_id=0):
148149
return self.sonar[sonar_id].get_distance()

rotary_encoder/rotarydecoder.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ def __init__(self, pi, feedback_pin_A, feedback_pin_B, callback):
3838
1 raising edge
3939
2 from watchdog
4040
41+
+---------+ +---------+ 0
42+
| | | |
43+
B | | | |
44+
| | | |
45+
+---------+ +---------+ +----- 1 # B leading A
46+
+---------+ +---------+ 0 # forward
47+
| | | |
48+
A | | | |
49+
| | | |
50+
----+ +---------+ +----------+ 1
51+
52+
4153
+---------+ +---------+ 0
4254
| | | |
4355
A | | | |
@@ -48,17 +60,6 @@ def __init__(self, pi, feedback_pin_A, feedback_pin_B, callback):
4860
B | | | |
4961
| | | |
5062
+---------+ +---------+ +----- 1
51-
52-
+---------+ +---------+ 0
53-
| | | |
54-
A | | | |
55-
| | | |
56-
+---------+ +---------+ +----- 1 # B leading A
57-
+---------+ +---------+ 0 # forward
58-
| | | |
59-
B | | | |
60-
| | | |
61-
----+ +---------+ +----------+ 1
6263
"""
6364
def _pulse(self, gpio, level, tick):
6465
# interrupt comes from pin A

rotary_encoder/wheelsaxel.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def __init__(self, pi, enable_pin,
4545
def distance(self):
4646
l_dist = self._left_motor.distance()
4747
r_dist = self._right_motor.distance()
48-
return (l_dist + r_dist) / 2
48+
return (l_dist + r_dist) * 0.5
4949

5050
#speed
5151
def speed(self):
5252
l_speed = self._left_motor.speed()
5353
r_speed = self._right_motor.speed()
54-
return (l_speed + r_speed) / 2
54+
return (l_speed + r_speed) * 0.5
5555

5656
# MOVEMENT
5757
""" Movement wrapper method
@@ -91,10 +91,8 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
9191
self._right_motor.control(power_right)
9292

9393
# moving for certaing amount of distance
94-
while(self.distance() < target_distance):
95-
sleep(0.05) # check if arrived every 50ms,
96-
print(str(self.distance()))
97-
target_distance = target_distance - self.distance() # updating target distance
94+
while(abs(self.distance()) < target_distance):
95+
pass # busy waiting
9896

9997
# robot arrived
10098
self.stop()
@@ -110,7 +108,6 @@ def control_velocity(self, time_elapse=0, target_distance=0):
110108
def stop(self):
111109
self._left_motor.stop()
112110
self._right_motor.stop()
113-
114111
self._is_moving = False
115112
self._wheelsAxle_lock.release()
116113

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from coderbot import CoderBot
2+
3+
c = CoderBot.get_instance();
4+
5+
c._twin_motors_enc.control_distance(100, 100, 200)
6+
7+
while(True):
8+
print(c._twin_motors_enc.distance())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from wheelsaxel import WheelsAxel
2+
import pigpio
3+
4+
# GPIO
5+
# motors
6+
PIN_MOTOR_ENABLE = 22
7+
PIN_LEFT_FORWARD = 25
8+
PIN_LEFT_BACKWARD = 24
9+
PIN_RIGHT_FORWARD = 4
10+
PIN_RIGHT_BACKWARD = 17
11+
#?
12+
PIN_PUSHBUTTON = 11
13+
# servo
14+
PIN_SERVO_3 = 9
15+
PIN_SERVO_4 = 10
16+
# sonar
17+
PIN_SONAR_1_TRIGGER = 18
18+
PIN_SONAR_1_ECHO = 7
19+
PIN_SONAR_2_TRIGGER = 18
20+
PIN_SONAR_2_ECHO = 8
21+
PIN_SONAR_3_TRIGGER = 18
22+
PIN_SONAR_3_ECHO = 23
23+
# encoder
24+
PIN_ENCODER_LEFT_A = 14
25+
PIN_ENCODER_LEFT_B = 6
26+
PIN_ENCODER_RIGHT_A = 15
27+
PIN_ENCODER_RIGHT_B = 12
28+
29+
pi = pigpio.pi()
30+
31+
twin_motors_enc = WheelsAxel(
32+
pi,
33+
enable_pin=PIN_MOTOR_ENABLE,
34+
left_forward_pin=PIN_LEFT_FORWARD,
35+
left_backward_pin=PIN_LEFT_BACKWARD,
36+
left_encoder_feedback_pin_A=PIN_ENCODER_LEFT_A,
37+
left_encoder_feedback_pin_B=PIN_ENCODER_LEFT_B,
38+
right_forward_pin=PIN_RIGHT_FORWARD,
39+
right_backward_pin=PIN_RIGHT_BACKWARD,
40+
right_encoder_feedback_pin_A=PIN_ENCODER_RIGHT_A,
41+
right_encoder_feedback_pin_B=PIN_ENCODER_RIGHT_B)
42+
43+
twin_motors_enc.control_distance(100, 100, 200)

v2.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,21 @@ paths:
209209
description: Movement speed and duration
210210
schema:
211211
type: object
212-
default: {'speed': 100, 'elapse':-1}
212+
default: {'speed': 100, 'elapse':0, 'distance':0}
213213
required:
214214
- speed
215215
- elapse
216+
- distance
216217
properties:
217218
speed:
218219
type: number
219220
description: 0 to 100 represent a forward movement (100 being the maximum speed), while 0 to -100 is a backward movement (-100 being the maximu speed)
220221
elapse:
221222
type: number
222-
description: Duration of the movement. -1 moves the bot until a /stop command is received.
223+
description: Duration of the movement. 0 doesn't move the coderbot.
224+
distance:
225+
type: number
226+
description: Target distance for the bot to travel
223227
responses:
224228
200:
225229
description: Sent command to the bot GPIO.

0 commit comments

Comments
 (0)