/*--------------------------------------------------------------------------- Jason Friskey EGR 474 - Systems Integration Dr. Hugh Jack Term: Spring/Summer 2003 File: robot_code.c Robot control program for workcell ---------------------------------------------------------------------------*/ #include #include #include #include #include #include "serial_io.h" #include serial_io *serial; PGresult *res; PGconn *conn; int check_db(); void write_db(); void run_robot(int); int main(){ int command; char query_string[256]; printf("Initializing serial connection \n"); serial = new serial_io("B9600,F/dev/ttyS0"); //initialize serial connection printf("Connecting to database \n"); conn = PQconnectdb("hostaddr=192.168.1.206 dbname=workcell user=friskeyj password=adidas"); printf("Checking connection \n"); while(PQstatus(conn) != CONNECTION_BAD){ sprintf(query_string, "SELECT status FROM Robot"); res = PQexec(conn, query_string); if(strncmp(PQgetvalue(res,0,0), "executing", 9) == 0){ printf("Checking robot status: [%s] \n", PQgetvalue(res,0,0)); command = check_db(); //retrieve current command from database printf("Executing load/unload tasks \n"); run_robot(command); //run robot_program based on command printf("Updating database \n"); write_db(); //update robot status to 'done' printf("Done updating database \n"); } //exectue if statement when robot status becomes 'executing' } //check for 'exectuing' robot status while connected to database printf("Disconnecting from database \n"); PQfinish(conn); delete serial; printf("Disconnecting from serial \n"); printf("Done \n"); return 0; } int check_db(){ char* command; char query_string1[256]; sprintf(query_string1, "SELECT command FROM Robot"); res = PQexec(conn, query_string1); if(PQresultStatus(res) == PGRES_TUPLES_OK){ command = PQgetvalue(res,0,0); PQclear(res); } else { printf("The query failed \n"); } return atoi(command); //return current robot command as an integer } //retreive current robot command from database void write_db(){ char query_string2[256]; sprintf(query_string2, "UPDATE Robot SET status = 'done'"); res = PQexec(conn, query_string2); if(PQresultStatus(res) == PGRES_COMMAND_OK){ PQclear(res); } else { printf("The database entry failed \n"); } } //update robot status in database to 'done' void run_robot(int command) { if(command == 1){ serial->writer("RN 20\n"); sleep(45); } if(command == 2){ serial->writer("RN 120\n"); sleep(30); } } //execute desired robot tasks based on command value retrieved from check_db()