Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I45 #57

Merged
merged 8 commits into from
Feb 1, 2019
Merged

I45 #57

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
can differentiate between black line and white paper
  • Loading branch information
cd6 committed Feb 1, 2019
commit bf609ca49890ea7b3010ced70732f0299e71a152
47 changes: 37 additions & 10 deletions ev3/detectLine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#! /usr/bin/env python3
import ev3dev.ev3 as ev3

class detectLine:

class DetectLine:
# Constructor
def __init__(self):
self.btn = ev3.Button()
Expand All @@ -11,20 +12,46 @@ def run(self):
cs = ev3.ColorSensor()
assert cs.connected

#cs.mode = 'COL-REFLECT' # measure light intensity
cs.mode = 'COL-COLOR' # measure colour
while(True):
yeet = cs.value()
print(yeet)
cs.mode = 'COL-REFLECT' # measure light intensity
#cs.mode = 'COL-COLOR' # measure colour

# motors
flm = ev3.LargeMotor('outA') # front-left motor
frm = ev3.LargeMotor('outB') # front-right motor
sm = ev3.LargeMotor('outC') # sidemotor
flm = ev3.LargeMotor('outA') # front-left motor
frm = ev3.LargeMotor('outB') # front-right motor
sm = ev3.LargeMotor('outC') # side motor
#sm.run_timed(speed_sp=300, time_sp=1000)

speed = 360 / 4 # deg/sec, [-1000, 1000]
dt = 500 # milliseconds
# stop_action = "coast"

# PID tuning
Kp = 1 # proportional gain
Ki = 0 # integral gain
Kd = 0 # derivative gain

integral = 0
previous_error = 0

# initial measurement when using one sensor to callibrate
target_value = cs.value()

# Start the main loop
while not self.shut_down:
# Calculate steering using PID algorithm
error = target_value - cs.value()
integral += (error * dt)
derivative = (error - previous_error) / dt

# u zero: on target, drive forward
# u positive: too bright, turn right
# u negative: too dark, turn left

u = (Kp * error) + (Ki * integral) + (Kd * derivative)
print(u)


# Main function
if __name__ == "__main__":
robot = detectLine()
robot = DetectLine()
robot.run()