Skip to content

Commit 891330b

Browse files
committed
wip PIDcontroller
1 parent 7a045ee commit 891330b

16 files changed

+2035
-2183
lines changed

cnn_models/models.json

+6
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"image_width": 224,
1717
"status": 1,
1818
"image_height": 224
19+
},
20+
"object_detect_mobile": {
21+
"output_layer": "final_result",
22+
"image_width": 224,
23+
"status": 1,
24+
"image_height": 224
1925
}
2026
}

main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def serve_legacy():
106106
config=app.bot_config,
107107
program_level=app.bot_config.get("prog_level", "std"),
108108
cam=Camera.get_instance() != None,
109-
cnn_model_names=json.dumps([[name] for name in CNNManager.get_instance().get_models().keys()]))
109+
cnn_model_names=json.dumps([[name, name] for name in CNNManager.get_instance().get_models().keys()]))
110110

111111
@babel.localeselector
112112
def get_locale():
@@ -198,7 +198,7 @@ def handle_bot():
198198
except:
199199
cam = None
200200
motion = None
201-
201+
202202
audio = Audio.get_instance()
203203

204204
cmd = request.args.get('cmd')

rotary_encoder/motorencoder.py

-46
Original file line numberDiff line numberDiff line change
@@ -149,54 +149,9 @@ def adjust_power(self, power):
149149
# releasing lock on motor
150150
self._motor_lock.release()
151151

152-
# CALLBACK
153-
""" The callback function rotary_callback is called on FALLING_EDGE by the
154-
rotary_decoder with a parameter value of 1 (1 new tick)
155-
156-
- Gearbox ratio: 120:1 (1 wheel revolution = 120 motor revolution)
157-
- Encoder ratio: 16:1 encoder ticks for 1 motor revolution
158-
- 1 wheel revolution = 120 * 16 = 1920 ticks
159-
- R = 30mm - 1 wheel revolution = 2πR = 2 * π * 30mm = 188.5mm
160-
- 1920 ticks = 188.5mm
161-
- 1 tick = 0.0981mm
162-
- 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately
163-
So 0.0981 is the ticks->distance(mm) conversion coefficient
164-
165-
The callback function calculates current velocity by taking groups of
166-
ticks_threshold ticks"""
167-
168-
# callback function
169-
# it calculates velocity via approssimation
170-
# it doeas a mean on current time passed and actual distance travelled
171-
# NOT USEFUL FOR VELOCITY REGULATION since we need to know the current
172-
# velocity updated each time
173-
def rotary_callback(self, tick):
174-
self._motor_lock.acquire()
175-
176-
# on first movement
177-
if(self._distance == 0):
178-
self._start_timer = time() # clock started
179-
180-
181-
self._ticks += tick # updating ticks
182-
self._distance = self._ticks * 0.0981 # (mm) travelled
183-
184-
self._power = power # setting current power
185-
186-
# adjusting power forward
187-
if (self._direction == 1):
188-
self._pi.set_PWM_dutycycle(self._forward_pin, abs(power))
189-
# adjusting power bacakward
190-
else:
191-
self._pi.set_PWM_dutycycle(self._backward_pin, abs(power))
192-
193-
# releasing lock on motor
194-
self._motor_lock.release()
195-
196152
# CALLBACK
197153
""" The callback function rotary_callback is called on FALLING_EDGE by the
198154
rotary_decoder with a parameter value of 1 (1 new tick)
199-
200155
- Gearbox ratio: 120:1 (1 wheel revolution = 120 motor revolution)
201156
- Encoder ratio: 16:1 encoder ticks for 1 motor revolution
202157
- 1 wheel revolution = 120 * 16 = 1920 ticks
@@ -205,7 +160,6 @@ def rotary_callback(self, tick):
205160
- 1 tick = 0.0981mm
206161
- 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately
207162
So 0.0981 is the ticks->distance(mm) conversion coefficient
208-
209163
The callback function calculates current velocity by taking groups of
210164
ticks_threshold ticks"""
211165
# callback function

rotary_encoder/wheelsaxel.py

+46-40
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
105105
#PID parameters
106106
# assuming that power_right is equal to power_left and that coderbot
107107
# moves at 11.5mm/s at full PWM duty cycle
108-
MAX_SPEED = 260
109-
TARGET_LEFT = (MAX_SPEED/100) * power_left #velocity [mm/s]
108+
MAX_SPEED = 180
109+
TARGET_LEFT = (MAX_SPEED / 100) * power_left #velocity [mm/s]
110110
TARGET_RIGHT = (MAX_SPEED / 100) * power_right # velocity [mm/s]
111111

112112
# SOFT RESPONSE
@@ -115,14 +115,14 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
115115
#KI = 0.005 # integral coefficient
116116

117117
# MEDIUM RESPONSE
118-
#KP = 0.8 #proportional coefficient
119-
#KD = 0.04 # derivative coefficient
120-
#KI = 0.02 # integral coefficient
118+
KP = 0.2 #proportional coefficient
119+
KD = 0.1 # derivative coefficient
120+
KI = 0.02 # integral coefficient
121121

122122
# STRONG RESPONSE
123-
KP = 0.9 # proportional coefficient
124-
KD = 0.05 # derivative coefficient
125-
KI = 0.03 # integral coefficient
123+
#KP = 0.9 # proportional coefficient
124+
#KD = 0.05 # derivative coefficient
125+
#KI = 0.03 # integral coefficient
126126

127127
SAMPLETIME = 0.1
128128

@@ -134,45 +134,51 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
134134
left_integral_error = 0
135135
right_integral_error = 0
136136

137+
power_left_norm = power_left
138+
power_right_norm = power_right
137139
# moving for certaing amount of distance
138140
while(abs(self.distance()) < target_distance):
139141
# PI controller
140-
141-
# relative error
142-
left_error = TARGET_LEFT - self._right_motor.speed()
143-
right_error = TARGET_RIGHT - self._left_motor.speed()
144-
145-
left_speed += (left_error * KP) + (left_derivative_error * KD) + (left_integral_error * KI)
146-
right_speed += (right_error * KP) + (right_derivative_error * KD) + (right_integral_error * KI)
147-
print("LEFT correction: %f" % (left_error * KP + left_derivative_error * KD + left_integral_error * KI))
148-
print("RIGHT correction: %f" % (right_error * KP + right_derivative_error * KD + right_integral_error * KI))
149-
150-
# conrispondent new power
151-
left_power = max(min(100 * left_speed / MAX_SPEED, 100), 0)
152-
right_power = max(min(100 * right_speed / MAX_SPEED, 100), 0)
153-
154-
print("Left SPEED: %f" % (self._right_motor.speed()))
155-
print("Right SPEED: %f" % (self._left_motor.speed()))
156-
print("Left POWER: %f" % (right_power))
157-
print("Right POWER: %f" % (left_power))
158-
print("")
159-
160-
# adjusting power on each motors
161-
self._left_motor.adjust_power(left_power)
162-
self._right_motor.adjust_power(right_power)
163-
164-
print("Left error: %f" % (left_error))
165-
print("Right error: %f" % (right_error))
166-
print("")
167-
142+
if(self._left_motor.speed() > 10 and self._right_motor.speed() > 10):
143+
# relative error
144+
left_error = (TARGET_LEFT - self._left_motor.speed())/TARGET_LEFT*100.0
145+
right_error = (TARGET_RIGHT - self._right_motor.speed())/TARGET_RIGHT*100.0
146+
147+
power_left = power_left_norm + (left_error * KP) + (left_derivative_error * KD) + (left_integral_error * KI)
148+
power_right = power_left_norm + (right_error * KP) + (right_derivative_error * KD) + (right_integral_error * KI)
149+
#print("LEFT correction: %f" % (left_error * KP + left_derivative_error * KD + left_integral_error * KI))
150+
#print("RIGHT correction: %f" % (right_error * KP + right_derivative_error * KD + right_integral_error * KI))
151+
152+
# conrispondent new power
153+
power_left_norm = max(min(power_left, 100), 0)
154+
power_right_norm = max(min(power_right, 100), 0)
155+
156+
#print("Left SPEED: %f" % (self._right_motor.speed()))
157+
#print("Right SPEED: %f" % (self._left_motor.speed()))
158+
#print("Left POWER: %f" % (right_power))
159+
#print("Right POWER: %f" % (left_power))
160+
#print("")
161+
print("ml:", int(self._right_motor.speed()), " mr: ", int(self._left_motor.speed()),
162+
" le:", int(left_error), " re: ", int(right_error),
163+
" ls: ", int(left_speed), " rs: ", int(right_speed),
164+
" lp: ", int(power_left), " rp: ", int(power_right))
165+
166+
# adjusting power on each motors
167+
self._left_motor.adjust_power(power_left_norm)
168+
self._right_motor.adjust_power(power_right_norm)
169+
170+
#print("Left error: %f" % (left_error))
171+
#print("Right error: %f" % (right_error))
172+
#print("")
173+
174+
left_derivative_error = left_error
175+
right_derivative_error = right_error
176+
left_integral_error += left_error
177+
right_integral_error += right_error
168178

169179
# checking each SAMPLETIME seconds
170180
sleep(SAMPLETIME)
171181

172-
left_derivative_error = left_error
173-
right_derivative_error = right_error
174-
left_integral_error += left_error
175-
right_integral_error += right_error
176182

177183
# robot arrived
178184
self.stop()

0 commit comments

Comments
 (0)