-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_LineTrace.py
38 lines (31 loc) · 1.29 KB
/
test_LineTrace.py
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
import etrobosim.ev3api as ev3
import etrobosim as ets
# ColorSensorのReflectを使ってP制御でライントレースする。
def calcPID(r, target=20, power=70,P=1.8):
p=r-target
left=power-P*p
right=power+P*p
return (int(left),int(right))
def pidControl(initARM_count=-50,initTAIL_count=0):
left,right=calcPID(colorSensor.getBrightness())
motorL.setPWM(left)
motorR.setPWM(right)
motorARM.setPWM(initARM_count-motorARM.getCount())
motorTAIL.setPWM(initTAIL_count-motorTAIL.getCount())
#print("MotorR={},MotorL={},MotorARM={},Color={}".format(motorR.getCount(),motorL.getCount(),motorARM.getCount(),colorSensor.getBrightness()))
motorR=ev3.Motor(ev3.ePortM.PORT_B,True,ev3.MotorType.LARGE_MOTOR)
motorL=ev3.Motor(ev3.ePortM.PORT_C,True,ev3.MotorType.LARGE_MOTOR)
motorARM=ev3.Motor(ev3.ePortM.PORT_A,True,ev3.MotorType.MEDIUM_MOTOR)
motorTAIL=ev3.Motor(ev3.ePortM.PORT_D,True,ev3.MotorType.LARGE_MOTOR)
motorR.reset()
motorL.reset()
colorSensor=ev3.ColorSensor(ev3.ePortS.PORT_2)
try:
controller=ets.Controller(ets.Course.LEFT)
controller.addHandlers([motorR,motorL,motorARM,motorTAIL,colorSensor])
controller.start(debug=True)
controller.runCyclic(pidControl)
controller.exit_process()
except KeyboardInterrupt:
controller.exit_process()
pass