RPR/02_Software/02_RaspberryPi/update_random.py

40 lines
1.3 KiB
Python
Raw Normal View History

# Creates instructions for the robot to move 360 degrees around, make photos of the ball-plate and write the ball_colors to the database
import MySQLdb
import random
db = MySQLdb.connect("localhost","root","root","rpr_robot" ) # Open database connection
cursor = db.cursor() # prepare a cursor object using cursor() method
# Get the number of rows in the database for the loop
cursor.execute('select * from ball_positions')
cursor.fetchall()
num_entries = cursor.rowcount - 1
# Define the possible colors of the balls
colors = ["blue", "red", "yellow", "cyan", "green"]
is_empty = 0;
# Go through array and write detected positions to database
for i in range(0, num_entries + 1):
# Ball-Position 29 must be empty at any time!
if i == 29:
is_empty = 1
color = "invalid"
else:
is_empty = 0
# Set random color -> Just for offline-programming!
color = random.choice(colors);
# Prepare SQL query to UPDATE required records
sql = "UPDATE ball_positions SET pos_isEmpty = %i, ball_color = '%s', ball_targetPos = 0, ball_prio = -1 WHERE pos_id = %i" % (is_empty, color, i)
try:
cursor.execute(sql) # Execute the SQL command
db.commit() # Commit your changes in the database
except:
print "Error: unable to update records"
db.rollback() # Rollback in case there is any error
db.close() # disconnect from server