Skip to content

Commit 716eddc

Browse files
committed
Added Missions.py for mission programs. Porting project from SB complete. Master program complete, with automatic menu creation. Mission subroutines must be named "a##<MisssionName>", where ## is a two digit number starting at 00, and creates the mission order. (alphabetacal)
1 parent d60688e commit 716eddc

File tree

4 files changed

+147
-27
lines changed

4 files changed

+147
-27
lines changed

FLLMaster/DriveLibraries.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class Robot:
99
from ev3dev2.sound import Sound
1010
from ev3dev2.button import Button
1111
from ev3dev2.led import Leds, LED_COLORS, LED_DEFAULT_COLOR, LED_GROUPS, LEDS
12+
from ev3dev2.display import Display
13+
import ev3dev2.fonts as fonts
1214
import math
13-
import time
1415

1516
def __init__(self, filename):
1617
from configparser import SafeConfigParser
@@ -22,6 +23,7 @@ def __init__(self, filename):
2223
self.spkr = self.Sound()
2324
self.btn = self.Button()
2425
self.led = self.Leds()
26+
self.disp = self.Display()
2527

2628
self.LeftMotor = eval(conf.get('Drivebase', 'LeftMotorPort'))
2729
self.RightMotor = eval(conf.get('Drivebase', 'RightMotorPort'))

FLLMaster/Main.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
#!/usr/bin/env python3
2-
from DriveLibraries import *
2+
from MenuLib import init
3+
import MenuLib
34
from time import sleep
4-
from ev3dev2.display import *
5-
disp = Display()
6-
7-
R1 = Robot('robot.cfg')
8-
R1.gs.calibrate()
9-
sleep(1)
10-
R1.spkr.beep()
11-
R1.reflectCal()
12-
R1.btn.wait_for_bump('enter')
135
# Write Code Here ----------------------------------------
146

7+
init()
8+
from MenuLib import *
9+
MenuLib.robot.reflectCal()
10+
1511
def left(state):
16-
pass
12+
if state:
13+
minusCount()
14+
display()
1715
def right(state):
18-
pass
19-
def up(state):
20-
pass
16+
if state:
17+
addCount()
18+
display()
2119
def down(state):
22-
pass
20+
if state:
21+
calibrate()
22+
def up(state):
23+
if state:
24+
abort()
25+
minusCount()
26+
display()
2327
def enter(state):
2428
if state:
25-
R1.DriveAtHeading(0, 50, 50, True)
29+
run()
30+
addCount()
31+
display()
2632
def backspace(state):
27-
pass
33+
if state:
34+
abort()
2835

29-
R1.btn.on_left = left
30-
R1.btn.on_right = right
31-
R1.btn.on_up = up
32-
R1.btn.on_down = down
33-
R1.btn.on_enter = enter
34-
R1.btn.on_backspace = backspace
36+
robot.btn.on_left = left
37+
robot.btn.on_right = right
38+
robot.btn.on_up = up
39+
robot.btn.on_down = down
40+
robot.btn.on_enter = enter
41+
robot.btn.on_backspace = backspace
3542

3643

3744
while True:
38-
R1.btn.process()
39-
40-
45+
robot.btn.process()
46+
loopIndex = (loopIndex + 1) % 100
47+
displaySensor()
48+
checkDrift()

FLLMaster/MenuLib.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from DriveLibraries import *
2+
import multiprocessing
3+
import Missions
4+
from time import sleep
5+
mission = None
6+
robot = None
7+
programs = []
8+
numPrograms = 0
9+
count = 1
10+
loopIndex = 0
11+
12+
def init():
13+
global programs
14+
global numPrograms
15+
global robot
16+
robot = Robot('robot.cfg')
17+
programs = dir(Missions)
18+
i = 0
19+
index = -1
20+
21+
length = len(programs)
22+
while i < length:
23+
if not ((programs[index][0] == "a") and (programs[index][1].isnumeric()) and (programs[index][2].isnumeric())):
24+
programs.pop(index)
25+
else:
26+
index -= 1
27+
i += 1
28+
numPrograms = len(programs)
29+
30+
robot.disp.clear()
31+
robot.disp.text_grid(programs[0][3:], font=robot.fonts.load('timR24'))
32+
33+
def runCurrentMission():
34+
method_to_call = getattr(Missions, programs[count - 1])
35+
method_to_call()
36+
robot.rm.off(brake=False)
37+
robot.lm.off(brake=False)
38+
39+
def run():
40+
robot.zeroGyro()
41+
global mission
42+
mission = multiprocessing.Process(target=runCurrentMission)
43+
mission.start()
44+
45+
def display():
46+
name = programs[count - 1][3:]
47+
robot.disp.text_grid(name, font=robot.fonts.load('timR24'))
48+
49+
def abort():
50+
mission.terminate()
51+
mission.join()
52+
robot.lm.off(brake=False)
53+
robot.rm.off(brake=False)
54+
55+
def addCount():
56+
global count
57+
count = (count % numPrograms) + 1
58+
59+
def minusCount():
60+
global count
61+
count = ((count + (numPrograms - 2)) % numPrograms) + 1
62+
63+
def checkDrift():
64+
ar = robot.gs.angle_and_rate
65+
rate = ar[1]
66+
if rate < -0.5 or rate > 0.5:
67+
robot.led.all_off()
68+
robot.led.set_color('LEFT', 'RED')
69+
robot.led.set_color('RIGHT', 'RED')
70+
else:
71+
robot.led.reset()
72+
73+
def displaySensor():
74+
if loopIndex == 0:
75+
robot.disp.rectangle(False, 0, 89, 177, 120, 'white', 'white')
76+
robot.disp.update
77+
robot.cs._ensure_mode(robot.cs.MODE_COL_REFLECT)
78+
robot.disp.text_pixels("P1, Color:" + str(robot.correctedRLI), False, 40, 90, font=robot.fonts.load('courR10'))
79+
robot.disp.text_pixels("P2, Gyro:" + str(robot.correctedAngle), False, 40, 100, font=robot.fonts.load('courR10'))
80+
robot.disp.update()
81+
82+
def calibrate():
83+
robot.led.all_off()
84+
robot.led.set_color('LEFT', 'YELLOW')
85+
robot.led.set_color('RIGHT', 'YELLOW')
86+
robot.spkr.beep('-f 369.99')
87+
robot.gs.calibrate()
88+
sleep(1)
89+
robot.spkr.beep()
90+
robot.led.reset()

FLLMaster/Missions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from DriveLibraries import *
2+
robot = Robot('robot.cfg')
3+
# Write Mission Programs Here ----------------------
4+
5+
def a00Star():
6+
robot.ArcTurn(-90, 30, 30)
7+
robot.ArcTurn(-90, 30, -30)
8+
robot.ArcTurn(-90, 30, 30)
9+
robot.ArcTurn(-90, 30, -30)
10+
def a05LineFollow():
11+
robot.LineFollow(50, 50, True)
12+
def a10Command_Key():
13+
robot.DriveAtHeading(0, 50, 30, False)
14+
robot.ArcTurn(270, 30, 30)
15+
robot.DriveAtHeading(270, 50, 30, False)
16+
robot.ArcTurn(270, 30, 30)
17+
robot.DriveAtHeading(270 * 2, 50, 30, False)
18+
robot.ArcTurn(270, 30, 30)
19+
robot.DriveAtHeading(270 * 3, 50, 30, True)
20+
robot.GyroTurn(0)

0 commit comments

Comments
 (0)