Skip to content

Commit 85b4f08

Browse files
committed
Added a reflectCal method to the Robot class which calibrates the color sensor RLI scale using black and white setpoints. Also added a correctedReflectedLightIntensity property which returns the scaled RLI using the calibration values from reflectCal. Started work on LineStop.
1 parent bdb44cb commit 85b4f08

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

FLLMaster/DriveLibraries.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Robot:
77
from ev3dev2.sensor.lego import ColorSensor, GyroSensor, InfraredSensor, TouchSensor, UltrasonicSensor
88
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3, INPUT_4
99
from ev3dev2.sound import Sound
10+
from ev3dev2.button import Button
1011
import math
12+
1113
def __init__(self, filename):
1214
from configparser import SafeConfigParser
1315
conf = SafeConfigParser()
@@ -16,6 +18,7 @@ def __init__(self, filename):
1618
self.ForFLL = bool(conf.get('Other', 'ForFLL') == "TRUE")
1719

1820
self.spkr = self.Sound()
21+
self.btn = self.Button()
1922
self.LeftMotor = eval(conf.get('Drivebase', 'LeftMotorPort'))
2023
self.RightMotor = eval(conf.get('Drivebase', 'RightMotorPort'))
2124
self.WheelCircumference = float(conf.get('Drivebase', 'WheelCircumference'))
@@ -68,6 +71,25 @@ def __init__(self, filename):
6871
#self.spkr.speak("Robot object instantiated")
6972
self.spkr.beep()
7073

74+
def reflectCal(self):
75+
global reflHighVal
76+
global reflLowVal
77+
global reflRate
78+
self.spkr.speak("Show me white")
79+
self.btn.wait_for_bump('enter')
80+
reflHighVal = (self.cs.reflected_light_intensity)
81+
self.spkr.beep()
82+
self.spkr.speak("Show me black")
83+
self.btn.wait_for_bump('enter')
84+
reflLowVal = self.cs.reflected_light_intensity
85+
self.spkr.beep()
86+
reflRate = 100 / (reflHighVal - reflLowVal)
87+
88+
@property
89+
def correctedReflectedLightIntensity(self):
90+
value = min([100, max([0, (self.cs.reflected_light_intensity * reflRate) + (-reflLowVal * reflRate)])])
91+
return(value)
92+
7193
def correctedAngle(self):
7294
# Multiply the gyro angle by -1 if the gyro is mounted upside-down relative to the motors in the robot.
7395
# GyroInvertedNum is set up in __init__()
@@ -318,4 +340,49 @@ def AuxMotorBumpStop(self, Speed, Threshold, Port):
318340
if Port == self.AuxMotor1:
319341
self.m1.off()
320342
else:
321-
self.m2.off()
343+
self.m2.off()
344+
345+
def LineStop(self, Heading, Speed, Stop):
346+
"""
347+
Moves the robot in a specified direction at a specified speed until a line (White-Black) is seen, while using the gyro sensor to keep the robot moving in a straight line.
348+
349+
``Heading``: The angle at which to drive, with the direction the gyro was last calibrated in being zero.
350+
``Speed``: The speed at which to drive, in motor percentage (same speed units as EV3-G). A negative value will make the robot drive backwards.
351+
``Stop``: Stop motors after completion. If ``FALSE``, motors will continue running after ``Distance`` has been traveled. Otherwise, motors will stop after ``Distance`` cm.
352+
"""
353+
# Ensure values are within reasonable limits, and change them if necessary (Idiotproofing).
354+
if Speed > 75:
355+
Speed = 75
356+
print("Speed must be between -75 and 75 (inclusive).")
357+
elif Speed < -75:
358+
Speed = -75
359+
print("Speed must be between -75 and 75 (inclusive).")
360+
361+
sign = Speed / abs(Speed)
362+
363+
364+
365+
# Initialize variables for PID control
366+
integral = 0.0
367+
last_error = 0.0
368+
derivative = 0.0
369+
# Check if the motors have gone far enough
370+
while avg < target:
371+
# Read the gyro
372+
current_angle = self.correctedAngle()
373+
374+
# Calculate the PID components
375+
error = current_angle - Heading
376+
integral = integral + error
377+
derivative = error - last_error
378+
last_error = error
379+
380+
# Calculate Steering value based on PID components and kp, ki, and kd
381+
turn_native_units = sign * max([min([(self.kp * error) + (self.ki * integral) + (self.kd * derivative), 100]), -100])
382+
383+
# Start the motors, using the steering value calculated by the PID control, and the input speed multiplied by speedMult (0 - 1, see above).
384+
self.steer.on(-turn_native_units, (Speed * speedMult))
385+
386+
# If the robot is to stop, stop the motors. Otherwise, leave the motors on and return.
387+
if not Stop == False:
388+
self.steer.stop()

FLLMaster/Main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
R1 = Robot('robot.cfg')
66
R1.gs.calibrate
77
R1.zeroGyro()
8-
#R1.spkr.speak("Starting")
8+
R1.spkr.speak("Starting")
99
R1.spkr.beep()
10-
R1.AuxMotorBumpStop(-50, 100, OUTPUT_A)
10+
R1.reflectCal()
11+
while True:
12+
R1.btn.wait_for_bump('enter')
13+
R1.spkr.beep()
14+
print(R1.correctedReflectedLightIntensity)
15+

FLLMaster/robot.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ TouchPort= 0
1818
UltrasonicPort=INPUT_3
1919

2020
[AuxMotors]
21-
AuxMotor1 = OUTPUT_D
22-
AuxMotor2 = OUTPUT_A
21+
AuxMotor1 = 0
22+
AuxMotor2 = 0
2323

2424
[Other]
2525
ForFLL = TRUE

0 commit comments

Comments
 (0)