Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from easygui import * #now with a GUI!
- portNum = 1 #keep track of the port(s) used
- def makeMotor(program, motor):
- global portNum
- with file('robotCode.c', 'r') as original: data = original.read() #read in the old data
- with file('robotCode.c', 'w') as modified: modified.write(motor + " = [port" + str(portNum) + "]\n" + data) #prepend the motor = port code
- portNum += 1
- taskTabs = 0
- loopTabs = 0 #to keep track of the number of tabs for each block of code
- def tab(which):
- global taskTabs
- global loopTabs
- tabString = ""
- if which == "task":
- for x in range(0, taskTabs):
- tabString += "\t"
- else:
- for x in range(0, loopTabs+taskTabs):
- tabString += "\t"
- return tabString
- def gui():
- msg = "Enter your task"
- title = "RobotC Gen"
- fieldNames = ["Button/ Joystick","Motor Name","Special","Power"]
- fieldValues = [] # we start with blanks for the values
- fieldValues = multenterbox(msg,title, fieldNames)
- # make sure that none of the fields was left blank
- while True: # do forever, until we find acceptable values and break out
- # look for errors in the returned values
- errmsg = ""
- if not fieldValues:
- return (None, None, None, None)
- for item in fieldValues:
- if item.strip() == "":
- if fieldNames[fieldValues.index(item)] != "Special":
- if fieldNames[fieldValues.index(item)] != "Power":
- errmsg = errmsg + ('"%s" is a required field.\n\n' % item)
- if errmsg == "":
- break # no problems found
- else:
- # show the box again, with the errmsg as the message
- fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)
- return fieldValues
- mainTask = []
- mainLoop = []
- def main():
- print "Hit \"cancel\" to end."
- global taskTabs
- global loopTabs
- global mainTask
- global mainLoop
- program = file("robotCode.c", "w")
- mainTask.append("task main(){\n")
- taskTabs += 1
- mainLoop.append(tab("task") + "while(true){\n")
- loopTabs += 1
- #mainTask.append(mainLoop)
- controllerBtns = dict() #a dictionary of all the buttons on the vexRT controller
- controllerJSticks = dict() #a dictionary of all the joysticks on the vexRT controller
- controllerJSticks["Ch1"] = "vexRT[Ch1]"
- controllerJSticks["Ch2"] = "vexRT[Ch2]"
- controllerJSticks["Ch3"] = "vexRT[Ch3]"
- controllerJSticks["Ch4"] = "vexRT[Ch4]"
- controllerBtns["5U"] = "vexRT[5U]"
- controllerBtns["5D"] = "vexRT[5D]"
- controllerBtns["6U"] = "vexRT[6U]"
- controllerBtns["6D"] = "vexRT[6D]"
- controllerBtns["7U"] = "vexRT[7U]"
- controllerBtns["7D"] = "vexRT[7D]"
- controllerBtns["7L"] = "vexRT[7L]"
- controllerBtns["7R"] = "vexRT[7R]"
- controllerBtns["8U"] = "vexRT[8U]"
- controllerBtns["8D"] = "vexRT[8D]"
- controllerBtns["8L"] = "vexRT[8L]"
- controllerBtns["8R"] = "vexRT[8R]"
- specKeyWords = []
- specKeyWords.append("const")
- motors = [] #a list of all of the motors
- while True:
- (key, motor, spec, power) = gui()
- if key in controllerBtns:
- button(program, key, motor, spec, power)
- elif key in controllerJSticks:
- joystick(program, key, motor, spec)
- else:
- for command in mainTask:
- program.write(command)
- for command in mainLoop:
- program.write(command)
- program.write(tab("loop") + "}\n") #close the main loop
- loopTabs -= 1
- program.write("}") #close the main task
- program.close()
- for motor in motors:
- makeMotor(program, motor)
- program.close()
- break
- def joystick(program, channel, motor, special=None):
- global taskTabs
- global loopTabs
- mainLoop.append(tab("loop") + motor + " = vexRT[" + channel + "];\n")
- def button(program, btn, motor, special=None, power=None):
- global taskTabs
- global loopTabs
- global mainTask
- global mainLoop
- if power == None:
- power = 127
- buttonState = "toggle" + btn
- if special:
- if special == "const":
- nonConst = None
- for item in mainLoop:
- if motor in item:
- nonConst = item
- mainLoop.remove(item)
- break
- if nonConst == None:
- print "You have not assigned what controls this motor normally."
- else:
- mainTask.append(tab("task") + "bool " + buttonState + " = false;\n")
- #Toggle constant power on/ off
- mainLoop.append(tab("loop") + "if (vexRT[" + btn + "]){\n")
- loopTabs += 1
- mainLoop.append(tab("loop") + "if(" + buttonState + "){\n")
- loopTabs += 1
- mainLoop.append(tab("loop") + buttonState + " = false;\n")
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- mainLoop.append(tab("loop") + "else{\n")
- loopTabs += 1
- mainLoop.append(tab("loop") + buttonState + " = true;\n")
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- #Apply the pressure
- mainLoop.append(tab("loop") + "if(" + buttonState + "){\n")
- loopTabs += 1
- mainLoop.append(tab("loop") + motor + " = " + str(power) + ";\n")
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- mainLoop.append(tab("loop") + "else{\n")
- loopTabs += 1
- mainLoop.append("\t" + nonConst)
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- loopTabs -= 1
- else:
- mainTask.append(tab("task") + "bool " + buttonState + " = false;\n")
- mainLoop.append(tab("loop") + "if (vexRT[" + btn + "]){\n") #if the button is pressed
- loopTabs += 1
- mainLoop.append(tab("loop") + "if(" + buttonState + "){\n") #check the toggle state - if true
- loopTabs += 1
- mainLoop.append(tab("loop") + buttonState + " = false;\n") #toggle to false
- mainLoop.append(tab("loop") + motor + " = 0;\n") #set the new motor power
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- mainLoop.append(tab("loop") + "else{\n") #if false
- loopTabs += 1
- mainLoop.append(tab("loop") + buttonState + " = true;\n") #toggle to true
- mainLoop.append(tab("loop") + motor + " = " + str(power) + ";\n") #set the new motor power
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- loopTabs -= 1
- mainLoop.append(tab("loop") + "}\n")
- #loopTabs -= 1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement