File size: 9,277 Bytes
4b876a7 | 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 | import pigpio
import threading
from time import sleep
import logging
from rotary_encoder.motorencoder import MotorEncoder
class WheelsAxel:
""" Class that handles both motor encoders, left and right
This class works like a wheels axle, coordinating left and right
wheels at the same time
It also tries to handle the inconsistent tension on wheels
that makes one wheel go slower than the other """
def __init__(self, pi, enable_pin,
left_forward_pin, left_backward_pin, left_encoder_feedback_pin_A, left_encoder_feedback_pin_B,
right_forward_pin, right_backward_pin, right_encoder_feedback_pin_A, right_encoder_feedback_pin_B):
# state variables
self._is_moving = False
# left motor
self._left_motor = MotorEncoder(pi,
enable_pin,
left_forward_pin,
left_backward_pin,
left_encoder_feedback_pin_A,
left_encoder_feedback_pin_B)
# right motor
self._right_motor = MotorEncoder(pi,
enable_pin,
right_backward_pin,
right_forward_pin,
right_encoder_feedback_pin_A,
right_encoder_feedback_pin_B)
# other
#self._wheelsAxle_lock = threading.RLock() # race condition lock
# STATE GETTERS
""" Distance and speed are calculated by a mean of the feedback
from the two motors """
def is_moving(self):
return self._left_motor.is_moving() or self._right_motor.is_moving()
# distance
def distance(self):
l_dist = self._left_motor.distance()
r_dist = self._right_motor.distance()
return (l_dist + r_dist) * 0.5
#speed
def speed(self):
l_speed = self._left_motor.speed()
r_speed = self._right_motor.speed()
return (l_speed + r_speed) * 0.5
#direction
def direction(self):
l_dir = self._left_motor.direction()
r_dir = self._right_motor.direction()
if(l_dir == r_dir):
return l_dir
else:
return 0
# MOVEMENT
""" Movement wrapper method
if time is specified and distance is not, control_time is called
if distance is specified and time is not, control_distance is called
if both distance and time are specified, control_velocity is called """
def control(self, power_left=100, power_right=100, time_elapse=0, target_distance=0):
if(time_elapse != 0 and target_distance == 0): # time
self.control_time(power_left, power_right, time_elapse)
elif(time_elapse == 0 and target_distance != 0): # distance
self.control_distance(power_left, power_right, target_distance)
else: # velocity
self.control_velocity(time_elapse, target_distance)
""" Motor time control allows the motors
to run for a certain amount of time """
def control_time(self, power_left=100, power_right=100, time_elapse=0):
#self._wheelsAxle_lock.acquire() # wheelsAxle lock acquire
# applying tension to motors
self._left_motor.control(power_left, -1)
self._right_motor.control(power_right, -1)
self._is_moving = True
# moving for desired time
# fixed for direct control that uses time_elapse -1 and stops manually
if(time_elapse > 0):
sleep(time_elapse)
self.stop()
""" Motor distance control allows the motors
to run for a certain amount of distance (mm) """
def control_distance(self, power_left=100, power_right=100, target_distance=0):
#self._wheelsAxle_lock.acquire() # wheelsAxle lock acquire
self._is_moving = True
# get desired direction from power, then normalize on power > 0
left_direction = power_left/abs(power_left)
right_direction = power_right/abs(power_right)
power_left = abs(power_left)
power_right = abs(power_right)
self._left_motor.reset_state()
self._right_motor.reset_state()
# applying tension to motors
self._left_motor.control(power_left * left_direction)
self._right_motor.control(power_right * right_direction)
#PID parameters
# assuming that power_right is equal to power_left and that coderbot
# moves at 11.5mm/s at full PWM duty cycle
MAX_SPEED = 180
target_speed_left = (MAX_SPEED / 100) * power_left #velocity [mm/s]
target_speed_right = (MAX_SPEED / 100) * power_right # velocity [mm/s]
# SOFT RESPONSE
#KP = 0.04 #proportional coefficient
#KD = 0.02 # derivative coefficient
#KI = 0.005 # integral coefficient
# MEDIUM RESPONSE
KP = 0.4 #proportional coefficient
KD = 0.1 # derivative coefficient
KI = 0.02 # integral coefficient
# STRONG RESPONSE
#KP = 0.9 # proportional coefficient
#KD = 0.05 # derivative coefficient
#KI = 0.03 # integral coefficient
SAMPLETIME = 0.01
left_derivative_error = 0
right_derivative_error = 0
left_integral_error = 0
right_integral_error = 0
# moving for certaing amount of distance
logging.info("moving? " + str(self._is_moving) + " distance: " + str(self.distance()) + " target: " + str(target_distance))
while(abs(self.distance()) < abs(target_distance) and self._is_moving == True):
# PI controller
logging.info("speed.left: " + str(self._left_motor.speed()) + " speed.right: " + str(self._right_motor.speed()))
if(abs(self._left_motor.speed()) > 10 and abs(self._right_motor.speed()) > 10):
# relative error
left_error = (target_speed_left - self._left_motor.speed()) / target_speed_left * 100.0
right_error = (target_speed_right - self._right_motor.speed()) / target_speed_right * 100.0
left_correction = (left_error * KP) + (left_derivative_error * KD) + (left_integral_error * KI)
right_correction = (right_error * KP) + (right_derivative_error * KD) + (right_integral_error * KI)
corrected_power_left = power_left + left_correction - right_correction
corrected_power_right = power_right + right_correction - left_correction
#print("LEFT correction: %f" % (left_error * KP + left_derivative_error * KD + left_integral_error * KI))
#print("RIGHT correction: %f" % (right_error * KP + right_derivative_error * KD + right_integral_error * KI))
# conrispondent new power
power_left_norm = max(min(corrected_power_left, 100), 0)
power_right_norm = max(min(corrected_power_right, 100), 0)
logging.info("ls:" + str(int(self._left_motor.speed())) + " rs: " + str(int(self._right_motor.speed())) +
" le:" + str(int(left_error)) + " re: " + str(int(right_error)) +
" lc: " + str(int(left_correction)) + " rc: " + str(int(right_correction)) +
" lp: " + str(int(power_left_norm)) + " rp: " + str(int(power_right_norm)))
# adjusting power on each motors
self._left_motor.adjust_power(power_left_norm * left_direction )
self._right_motor.adjust_power(power_right_norm * right_direction)
left_derivative_error = left_error
right_derivative_error = right_error
left_integral_error += left_error
right_integral_error += right_error
# checking each SAMPLETIME seconds
sleep(SAMPLETIME)
logging.info("control_distance.stop, target dist: " + str(target_distance) +
" actual distance: " + str(self.distance()) +
" l ticks: " + str(self._left_motor.ticks()) +
" r ticks: " + str(self._right_motor.ticks()))
# robot arrived
self.stop()
""" Motor speed control to travel given distance
in given time adjusting power on motors
NOT very intuitive, idea has been postponed"""
def control_velocity(self, time_elapse=0, target_distance=0):
pass
""" The stop function calls the two stop functions of the two
correspondent motors.
Locks are automatically obtained """
def stop(self):
# stopping left and right motors
self._left_motor.stop()
self._right_motor.stop()
# trying to fix distance different than zero after
# wheels has stopped by re-resetting state after 0.5s
#self._left_motor.reset_state()
#self._right_motor.reset_state()
# updating state
logging.info("stopping")
self._is_moving = False
# CALLBACK
def cancel_callback(self):
self._right_motor.cancel_callback()
self._left_motor.cancel_callback()
|