11#!/usr/bin/env python3
22
33from configparser import ConfigParser
4- from ev3dev2 import sensor
54from ev3dev2 .display import *
65from ev3dev2 .button import *
76from ev3dev2 .sensor import lego
8- import ev3dev2 .sensor .lego
97from ev3dev2 .motor import *
108import re
9+ from time import sleep
1110
1211
1312def configBuilder (configFile = 'robot.cfg' ):
1413 """
1514 Creates a robot config file based on user input and sensor/motor values
1615 """
1716 global lego
17+ btn = Button ()
18+ disp = Display ()
1819 config = ConfigParser ()
1920 config ['Drivebase' ] = {}
2021 config ['Sensors' ] = {}
2122 config ['AuxMotors' ] = {}
2223 config ['Other' ] = {}
24+ outPorts = ['OUTPUT_A' , 'OUTPUT_B' , 'OUTPUT_C' , 'OUTPUT_D' ]
2325 senTypes = ['Color' , 'Gyro' , 'Touch' , 'Ultrasonic' , 'Infrared' ]
2426
2527 for i in senTypes :
@@ -28,16 +30,42 @@ def configBuilder(configFile='robot.cfg'):
2830 mySensor = sensorClass ()
2931 port = str (mySensor .address )
3032 port = re .sub ('.*in([0-9]).*' , 'INPUT_\g<1>' , port )
33+ globals ()[i ] = port
3134 config ['Sensors' ][i + 'Port' ] = port
3235 except :
3336 config ['Sensors' ][i + 'Port' ] = 'None'
3437 finally :
3538 mySensor = None
3639 sensorClass = None
3740
41+ for i in outPorts :
42+ try :
43+ motor = LargeMotor (eval (i ))
44+ disp .text_pixels ("Found a Large Motor at " + "port " + i [- 1 ])
45+ disp .text_pixels ("What kind of motor is this?" , False , 0 , 10 )
46+ disp .text_pixels ("Press left for left motor," , False , 0 , 20 )
47+ disp .text_pixels ("right for right motor," , False , 0 , 30 )
48+ disp .text_pixels ("up for Aux1, or down for Aux2" , False , 0 , 40 )
49+ disp .update ()
50+ while not btn .any ():
51+ pass
52+ selection = btn .buttons_pressed
53+ if selection [0 ] == 'left' :
54+ btn .wait_for_released ('left' )
55+ lm = motor
56+ config ['Drivebase' ]['LeftMotorPort' ] = i
57+ elif selection [0 ] == 'right' :
58+ btn .wait_for_released ('right' )
59+ rm = motor
60+ config ['Drivebase' ]['RightMotorPort' ] = i
61+
62+ except :
63+ pass
64+
3865 with open (configFile , 'w' ) as configfile :
3966 config .write (configfile )
4067
68+
4169if __name__ == "__main__" :
4270 configBuilder ('robot2.cfg' )
4371
0 commit comments