36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
# Reads the positions of all sorted balls from the database and calls create_instr so the robot sorts them
|
|
|
|
import MySQLdb
|
|
from create_instructions import create_instr
|
|
|
|
db = MySQLdb.connect("localhost","root","root","rpr_robot" ) # Open database connection
|
|
cursor = db.cursor() # Prepare a cursor object using cursor() method
|
|
|
|
# Write the sql-query for later proceeding
|
|
# Get the initial and final position as well as the id of all balls which are placed and not sorted by the robot yet, ordered by the priority ascending
|
|
sql = "SELECT pos_x, pos_y, ball_targetPos, pos_id FROM ball_positions WHERE ball_targetPos!=0 AND sorted_by_robot=0 ORDER BY ball_prio ASC"
|
|
|
|
try:
|
|
cursor.execute(sql) # Execute the SQL command
|
|
results = cursor.fetchall() # Fetch all the rows
|
|
for row in results:
|
|
posX = row[0]
|
|
posY = row[1]
|
|
ball_targetPos = row[2]
|
|
id = row[3]
|
|
print ball_targetPos
|
|
create_instr(posX, posY, ball_targetPos) # Create movement-instructions for every sorted ball
|
|
sql = "UPDATE ball_positions SET sorted_by_robot=1 WHERE pos_id = %i" % (id) # Write to database, that the robot moved this ball in real life
|
|
cursor.execute(sql) # Execute the SQL command
|
|
db.commit() # Commit your changes in the database
|
|
except:
|
|
print "Error: unable to fetch data"
|
|
db.rollback() # Rollback in case there is any error
|
|
|
|
db.close() # disconnect from server
|
|
|
|
# For debugging
|
|
file = open("testfile.txt", "w")
|
|
file.write("readBalls")
|
|
file.close
|