Sarah Prior▸ Task 6 – Implementing Your Game
Below is a program written by a student in my Robotics class. He is using a BrickPi to program Lego Robotics motors and sensors to get it to perform as a SumoBot (robot battles). It never was as powerful as the other robots but it is the first time I have ever had a student write a script in python that worked and I actually learned a lot from him. Below are his words and the questions I put to him are interspersed in his code below as comments. #exploreTask6
Student comment: My new and improved Sumo-Bot program that works perfectly, instead of using colour to tell the Sumo-Bot if it was in the ring or not I found out that I can use reflected light with the colour sensor, which was much easier to do and now I have a really good Sumo-Bot, another thing as well where the colour sensor is setup and has 'RED' at the end if you change that to any of the other coloured LED lights that is in the colour you can use those as well to see reflected light.
>>> from BrickPi import*
>>> BrickPiSetup() # Now I understand this is a function from the BrickPi library import. I would like to see that bit of code, can you track this function down? I would assume we can set all sensors/motors but perhaps there is more we can setup?
>>> BrickPi.SensorType[PORT_1] = TYPE_SENSOR_COLOR_RED #So you are telling me any colour could be listed here and we can still use reflected light values?
>>> BrickPi.SensorType[PORT_2] = TYPE_SENSOR_ULTRASONIC_CONT What is CONT (perhaps continuous?)
>>> BrickPi.MotorEnable[PORT_A] = 1 #I assume 1 is on and 0 is off??
>>> BrickPi.MotorEnable[PORT_B] = 1
>>> BrickPiSetupSensors() #Again, I would like to see the function to know what arguments can be defined if you get my point. Maybe it is far too long a script to ever fully understand? We are using it here to tell the sensors what to respond to from this point forward?
>>> def search(): #This is your first homemade function!! That is so exciting. You don't define any arguments (nothing in brackets), but you put in the switch we have in our EV3 program for the ultrasonic.
BrickPi.MotorSpeed[PORT_A] = 255 #both motors full power (straight ahead)
BrickPi.MotorSpeed[PORT_B] = 255
BrickPi.MotorSpeed[PORT_A] = 100 #with motor powered in opposite direction, robot will turn continuously
BrickPi.MotorSpeed[PORT_B] = -100
BrickPiUpdateValues() #I really want to know why we need this function here? Does this set the values for the sensors to be called below only when it doesn't see the opponent
>>> while True: #This is where we set up a loop in that while this condition below is true (or not) the robot will do either task.
result = BrickPiUpdateValues() #are you using the BrickPiUpdateValues() set above when the robot does not see the opponent, so you can test if the light condition is true below?
if not result: #"if not result" actually means: result has not happened (not seeing opponent) so we must be seeing the opponent?!
print BrickPi.Sensor[PORT_1] #here we are writing on the screen so we know the value the robot is measuring for troubleshooting
if BrickPi.Sensor[PORT_1]>500: #here we are checking to see if light levels are "white"
search() #search function above is called if we are on white and motors will be activated if opponent is detected
else: #else means: robot does not see opponent
if BrickPi.Sensor[PORT_1]<500: query if light sensor is less than 500 (must be black?)
BrickPi.MotorSpeed[PORT_A] = -255 both motors go back full power to get into the ring
BrickPi.MotorSpeed[PORT_B] = -255
time.sleep(0.1) a slight pause before starting again
no plus ones, 0 comments
You must log in to post a comment.
+ There are no commentsAdd yours