#include #include #include #include #include #include "cgic.h" #define NUM_MOTORS 4 #define MAX_BASE_POS 2370 #define MAX_BICEP_POS 770 #define MAX_FOREARM_POS 560 #define MAX_CLAW_POS 600 // prototypes int shouldMotorStop(int motor, int dir, int on); /** * Holds state for each motor. */ struct MotorState { int on; int dir; int pos; int maxPos; }; struct MotorState baseMotorState; struct MotorState bicepMotorState; struct MotorState forearmMotorState; struct MotorState clawMotorState; struct MotorState motorStates[4]; /** * Initializes the motor states by reading them from the device driver. */ void initMotorStates() { int file = open("/dev/robarm0", O_RDONLY); char* msg; int *a; int len = (12 * sizeof(int)); msg = (char*) malloc(len); read(file, msg, len); close(file); baseMotorState.on = *((int*) msg + 0); baseMotorState.dir = *((int*) msg + 1); baseMotorState.pos = *((int*) msg + 2); baseMotorState.maxPos = MAX_BASE_POS; motorStates[0] = baseMotorState; bicepMotorState.on = *((int*) msg + 3); bicepMotorState.dir = *((int*) msg + 4); bicepMotorState.pos = *((int*) msg + 5); bicepMotorState.maxPos = MAX_BICEP_POS; motorStates[1] = bicepMotorState; forearmMotorState.on = *((int*) msg + 6); forearmMotorState.dir = *((int*) msg + 7); forearmMotorState.pos = *((int*) msg + 8); forearmMotorState.maxPos = MAX_FOREARM_POS; motorStates[2] = forearmMotorState; clawMotorState.on = *((int*) msg + 9); clawMotorState.dir = *((int*) msg + 10); //clawMotorState.pos = *((int*) msg + 11); clawMotorState.pos = 8; // we don't really care about the claw's position, as long as it's not zero clawMotorState.maxPos = MAX_CLAW_POS; motorStates[3] = clawMotorState; free(msg); } /** * Writes a motor command to the device driver. */ void executeMotorCommand(int motor, int dir, int on) { int file = open("/dev/robarm0", O_RDWR); char msg[3]; if (shouldMotorStop(motor, dir, on) == 1) { msg[0] = motor + '0'; msg[1] = '0'; msg[2] = '0'; } else { msg[0] = (char) motor + '0'; msg[1] = (char) on + '0'; msg[2] = (char) dir + '0'; } write(file, msg, 3); close(file); } /** * Checks if a motor should continue in the given direction. */ int shouldMotorStop(int motor, int dir, int on) { // special case /*if (motor == 3) { if (motorStates[3].pos <= 0 && dir == 0) { return 1; } else if (motorStates[3].pos >= motorStates[3].maxPos && dir == 1) { return 1; } else { return 0; } } else */if ((dir == 1 && motorStates[motor].pos >= motorStates[motor].maxPos) || (dir == 0 && motorStates[motor].pos <= 0)) { return 1; } else { return 0; } } int cgiMain() { char *baseMotorName = "Base Motor"; char *bicepMotorName = "Bicep Motor"; char *forearmMotorName = "Forearm Motor"; char *clawMotorName = "Claw Motor"; char *motorNames[] = {baseMotorName, bicepMotorName, forearmMotorName, clawMotorName}; int motorCommand = 1; int motor, direction, on; int i; initMotorStates(); if (cgiFormInteger("motor", &motor, -1) != cgiFormSuccess) { motorCommand = 0; } if (motorCommand == 1 && cgiFormInteger("direction", &direction, -1) != cgiFormSuccess) { motorCommand = 0; } if (motorCommand == 1 && cgiFormInteger("on", &on, -1) != cgiFormSuccess) { motorCommand = 0; } cgiHeaderContentType("text/html"); fprintf(cgiOut, " "); fprintf(cgiOut, "\n"); fprintf(cgiOut, "Evil Robot Arm\n"); // if a motor is running if (motorCommand == 1 && on == 1 && shouldMotorStop(motor, direction, on) == 0) { fprintf(cgiOut, "", motor, direction, on); } fprintf(cgiOut, " "); fprintf(cgiOut, "\n"); fprintf(cgiOut, "

Evil Robot Arm Control Panel

"); for (i = 0; i < NUM_MOTORS; i++) { int j; fprintf(cgiOut, ""); } fprintf(cgiOut, "
", motorNames[i]); if (motorCommand == 1 && motor == i && on == 1 && shouldMotorStop(motor, direction, on) == 0) { if (direction == 0) { fprintf(cgiOut, " ", i, i, i); } else { fprintf(cgiOut, " ", i, i, i); } } else { fprintf(cgiOut, " ", i, i, i); } // here's the position stuff if (i != 3) { fprintf(cgiOut, "
%s
>\" onclick=\"executeMotorCommand(%i, 1, 1);\"> >\" style=\"color:green; background-color:yellow\" onclick=\"executeMotorCommand(%i, 1, 1);\"> >\" onclick=\"executeMotorCommand(%i, 1, 1);\">
"); // BEGIN SIMPLE //fprintf(cgiOut, "Position [%i]", motorStates[i].pos); // END SIMPLE // BEGIN COMPLICATED fprintf(cgiOut, ""); for (j = 0; j < motorStates[i].maxPos; j += (motorStates[i].maxPos/20)) { if (j < motorStates[i].pos) { fprintf(cgiOut, ""); } else { fprintf(cgiOut, ""); } } fprintf(cgiOut, "
xo
"); // END COMPLICATED } fprintf(cgiOut, "
"); fprintf(cgiOut, "

\"Valid

"); if (motorCommand == 1) { executeMotorCommand(motor, direction, on); } fprintf(cgiOut, "\n"); return 0; }