Skip to content

Commit

Permalink
Issue #1656: Add speed limit for txt motor control
Browse files Browse the repository at this point in the history
  • Loading branch information
heini208 committed May 15, 2024
1 parent 82a4840 commit 49a4539
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Sensors:
Actors:
[UsedActor [DIFFERENTIALDRIVE, DIFFERENTIALDRIVE], UsedActor [NO PORT, ENCODERMOTOR], UsedActor [NO PORT, DIFFERENTIALDRIVE], UsedActor [_D, ENCODERMOTOR], UsedActor [_D, DIFFERENTIALDRIVE]]
Methods:
[DIFFDRIVETURNDEGREES, DIFFERENTIALDRIVE, DIFFERENTIALDRIVECURVE, DIFFERENTIALDRIVEDISTANCE, MOTORSTART]
[DIFFDRIVETURNDEGREES, DIFFERENTIALDRIVE, DIFFERENTIALDRIVECURVE, DIFFERENTIALDRIVEDISTANCE, MOTORSTART, SPEEDTOPWM]
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Sensors:
Actors:
[UsedActor [M1, ENCODERMOTOR], UsedActor [M2, ENCODERMOTOR], UsedActor [ML, ENCODERMOTOR], UsedActor [MR, ENCODERMOTOR], UsedActor [S, SERVOMOTOR]]
Methods:
[MOTORSTART, MOTORSTARTFOR]
[MOTORSTART, MOTORSTARTFOR, SPEEDTOPWM]
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Sensors:
Actors:
[UsedActor [OMNIDRIVE, OMNIDRIVE], UsedActor [_O, ENCODERMOTOR], UsedActor [_O, OMNIDRIVE], UsedActor [no port, ENCODERMOTOR], UsedActor [no port, OMNIDRIVE]]
Methods:
[MOTORSTART, OMNIDRIVECURVE, OMNIDRIVECURVEDISTANCE, OMNIDRIVESTRAIGHTDISTANCE, OMNIDRIVETURNDEGREES]
[MOTORSTART, OMNIDRIVECURVE, OMNIDRIVECURVEDISTANCE, OMNIDRIVESTRAIGHTDISTANCE, OMNIDRIVETURNDEGREES, SPEEDTOPWM]
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Sensors:
Actors:
[UsedActor [DISPLAY, DISPLAY], UsedActor [M1, ENCODERMOTOR], UsedActor [M2, ENCODERMOTOR]]
Methods:
[MOTORSTART]
[MOTORSTART, SPEEDTOPWM]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def diffdrive_turn_degrees(speed, degrees):
if degrees < 0:
speed = -speed
degrees = abs(degrees)
speed = int((speed / 100) * 512)
speed = speed_to_pwm(speed)
arc_length = math.radians(degrees) * (TRACK_WIDTH / 2)
rotations = arc_length / (WHEEL_DIAMETER * math.pi)
steps_per_wheel = int(rotations * STEPS_PER_ROTATION)
Expand All @@ -43,7 +43,7 @@ def diffdrive_turn_degrees(speed, degrees):
left_motor.stop_sync(right_motor)

def diffdrive(speed):
speed = int((speed / 100) * 512)
speed = speed_to_pwm(speed)
left_motor.set_speed(speed, Motor.CCW)
right_motor.set_speed(speed, Motor.CCW)
left_motor.start_sync(right_motor)
Expand Down Expand Up @@ -71,9 +71,13 @@ def diffdrive_distance(distance, speed_l, speed_r):
time.sleep(0.010)

def motor_start(motor, speed):
motor.set_speed(int((speed / 100) * 512), Motor.CCW)
motor.set_speed(speed_to_pwm(speed), Motor.CCW)
motor.start()

def speed_to_pwm(speed):
speed = max(min(speed, 100), -100)
return int((speed / 100) * 512)

def run():
print("Driving Forwards")
diffdrive(30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@


def motor_start(motor, speed):
motor.set_speed(int((speed / 100) * 512), Motor.CCW)
motor.set_speed(speed_to_pwm(speed), Motor.CCW)
motor.start()

def motor_start_for(motor, speed, degrees):
steps = int((degrees / 360) * STEPS_PER_ROTATION)
motor.set_speed(int((speed / 100) * 512), Motor.CCW)
motor.set_speed(speed_to_pwm(speed), Motor.CCW)
motor.set_distance(steps)
while True:
if not motor.is_running():
break
time.sleep(0.010)
motor.stop()

def speed_to_pwm(speed):
speed = max(min(speed, 100), -100)
return int((speed / 100) * 512)

def run():
print("Moving Motors")
print("M1 Motor for 2 seconds")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


def motor_start(motor, speed):
motor.set_speed(int((speed / 100) * 512), Motor.CCW)
motor.set_speed(speed_to_pwm(speed), Motor.CCW)
motor.start()

def omnidrive_curve(speed_fl, speed_fr, speed_rl, speed_rr):
Expand Down Expand Up @@ -69,10 +69,10 @@ def omnidrive_straight_distance(distance, speed_fl, speed_fr, speed_rl, speed_rr
constant = WHEEL_DIAMETER / (2 * 4)
distance_per_wheel = distance / constant / 4
steps_per_wheel = int(distance_per_wheel / (2 * math.pi) * STEPS_PER_ROTATION)
front_left_motor.set_speed(int((direction * speed_fl / 100) * 512), Motor.CCW)
front_right_motor.set_speed(int((direction * speed_fr / 100) * 512), Motor.CCW)
rear_left_motor.set_speed(int((direction * speed_rl / 100) * 512), Motor.CCW)
rear_right_motor.set_speed(int((direction * speed_rr / 100) * 512), Motor.CCW)
front_left_motor.set_speed(speed_to_pwm(direction * speed_fl), Motor.CCW)
front_right_motor.set_speed(speed_to_pwm(direction * speed_fr), Motor.CCW)
rear_left_motor.set_speed(speed_to_pwm(direction * speed_rl), Motor.CCW)
rear_right_motor.set_speed(speed_to_pwm(direction * speed_rr), Motor.CCW)
front_left_motor.set_distance(steps_per_wheel, front_right_motor, rear_left_motor, rear_right_motor)
while True:
if (not front_left_motor.is_running()
Expand All @@ -87,7 +87,7 @@ def omnidrive_turn_degrees(speed, degrees):
if degrees < 0:
speed = -speed
degrees = abs(degrees)
speed = int((speed / 100) * 512)
speed = speed_to_pwm(speed)
rotations = (WHEEL_DIAMETER * math.pi * degrees) / (360 * TRACK_WIDTH)
steps_per_wheel = int(rotations * STEPS_PER_ROTATION)

Expand All @@ -105,6 +105,10 @@ def omnidrive_turn_degrees(speed, degrees):
time.sleep(0.010)
front_left_motor.stop_sync(front_right_motor, rear_left_motor, rear_right_motor)

def speed_to_pwm(speed):
speed = max(min(speed, 100), -100)
return int((speed / 100) * 512)

def run():
print("Driving Forwards")
omnidrive_curve(30, 30, 30, 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
_timer5 = time.time()

def motor_start(motor, speed):
motor.set_speed(int((speed / 100) * 512), Motor.CCW)
motor.set_speed(speed_to_pwm(speed), Motor.CCW)
motor.start()

def speed_to_pwm(speed):
speed = max(min(speed, 100), -100)
return int((speed / 100) * 512)

def run():
global _timer1, _timer2, _timer3, _timer4, _timer5
while not(TXT_M_I3_ultrasonic_distance_meter):
Expand Down
Loading

0 comments on commit 49a4539

Please sign in to comment.