main.cpp
00001 #include "scorbot.h"
00002 #include "buffered_serial.h"
00003
00004 #define READ_ENCODER_VALUE 10
00005 #define READ_MICROSWITCHES 11
00006 #define READ_PWM_VALUE 12
00007 #define READ_TRAJECTORY 13
00008 #define READ_CONTROL_PARAMS 15
00009 #define SET_DESTINATION 20
00010 #define RESET_ENCODER 21
00011 #define READ_ALL_ENCODERS 22
00012 #define READ_ALL_PWMS 23
00013 #define SET_CONTROL_PARAMS 25
00014 #define ENABLE_MOTORS 30
00015 #define DISABLE_MOTORS 31
00016
00017 #define LOOP_STATUS_TEXT 'l'
00018 #define DEBUG 'd'
00019
00020 #define FAILURE 254
00021 #define SUCCESS 255
00022
00023 BufferedSerial pc(USBTX, USBRX, 128);
00024
00025
00026
00027 int main()
00028 {
00029 Scorbot arm;
00030
00031
00032 pc.baud(115200);
00033
00034 uint8_t command, motor, length, index;
00035 uint8_t * param_buffer;
00036 long position, duration;
00037
00038 while (1)
00039 {
00040 command = pc.getc();
00041
00042 switch (command)
00043 {
00044 case READ_ENCODER_VALUE:
00045 motor = pc.getc();
00046 if (motor < NUM_MOTORS)
00047 {
00048 pc.writeLong(arm.readEncoder(motor));
00049 pc.putc(SUCCESS);
00050 }
00051 else
00052 {
00053 pc.writeLong(0);
00054 pc.putc(FAILURE);
00055 }
00056 break;
00057 case READ_MICROSWITCHES:
00058 pc.putc(arm.readMicroswitches());
00059 pc.putc(SUCCESS);
00060 break;
00061 case READ_PWM_VALUE:
00062 motor = pc.getc();
00063 if (motor < NUM_MOTORS)
00064 {
00065 pc.writeLong((long)(arm.readPWMDuty(motor) * 100000.0));
00066 pc.putc(SUCCESS);
00067 }
00068 else
00069 {
00070 pc.writeLong(0);
00071 pc.putc(FAILURE);
00072 }
00073 break;
00074 case READ_TRAJECTORY:
00075 motor = pc.getc();
00076 if (motor < NUM_MOTORS)
00077 {
00078 pc.writeLong(arm.readTargetPosition(motor));
00079 pc.writeLong(arm.readTargetVelocity(motor));
00080 pc.putc(SUCCESS);
00081 }
00082 else
00083 pc.putc(FAILURE);
00084
00085 break;
00086 case READ_CONTROL_PARAMS:
00087 motor = pc.getc();
00088 if (motor < NUM_MOTORS)
00089 {
00090 param_buffer = arm.readControlParameters(motor, length);
00091 pc.putc(length);
00092 for (index = 0; index < length; index++)
00093 pc.putc(param_buffer[index]);
00094 delete[] param_buffer;
00095 pc.putc(SUCCESS);
00096 }
00097 else
00098 pc.putc(FAILURE);
00099 break;
00100
00101 case SET_DESTINATION:
00102 motor = pc.getc();
00103 position = pc.readLong();
00104 duration = pc.readLong();
00105 if (motor < NUM_MOTORS)
00106 {
00107 arm.setDestination(motor, position, duration);
00108 pc.putc(SUCCESS);
00109 }
00110 else
00111 pc.putc(FAILURE);
00112 break;
00113
00114 case RESET_ENCODER:
00115 arm.resetEncoders();
00116 pc.putc(SUCCESS);
00117 break;
00118
00119 case READ_ALL_ENCODERS:
00120 pc.putc(READ_ALL_ENCODERS);
00121 pc.putc(4*NUM_MOTORS);
00122 for(index=0; index<NUM_MOTORS; index++)
00123 pc.writeLong(arm.readEncoder(index));
00124 pc.putc(255);
00125 break;
00126 case READ_ALL_PWMS:
00127 pc.putc(READ_ALL_PWMS);
00128 pc.putc(4*NUM_MOTORS);
00129 for (index = 0; index < NUM_MOTORS; index++)
00130 pc.writeLong((long)(arm.readPWMDuty(index) * 100000.0));
00131 pc.putc(255);
00132 break;
00133
00134 case SET_CONTROL_PARAMS:
00135 motor = pc.getc();
00136 length = pc.getc();
00137 param_buffer = new uint8_t[length];
00138 pc.readBytes(param_buffer, length);
00139 if (motor < 7 && arm.setControlParameters(motor, length, param_buffer))
00140 pc.putc(SUCCESS);
00141 else
00142 pc.putc(FAILURE);
00143 break;
00144
00145 case ENABLE_MOTORS:
00146 arm.enableMotors();
00147 pc.putc(SUCCESS);
00148 break;
00149
00150 case DISABLE_MOTORS:
00151 arm.disableMotors();
00152 pc.putc(SUCCESS);
00153 break;
00154
00155 case LOOP_STATUS_TEXT:
00156 while (!pc.readable())
00157 {
00158 pc.printf("--------------------------------------------------------------------------------\n");
00159 pc.printf(" Scorbot Status \n");
00160 pc.printf("--------------------------------------------------------------------------------\n");
00161 pc.printf("\tEncoder 1 = %d\t\t\tEncoder 2 = %d\n", arm.readEncoder(0), arm.readEncoder(1));
00162 pc.printf("\tEncoder 3 = %d\t\t\tEncoder 4 = %d\n", arm.readEncoder(2), arm.readEncoder(3));
00163 pc.printf("\tEncoder 5 = %d\t\t\tEncoder 6 = %d\n", arm.readEncoder(4), arm.readEncoder(5));
00164 pc.printf("\tEncoder 7 = %d\n", arm.readEncoder(6));
00165 pc.printf("--------------------------------------------------------------------------------\n");
00166 pc.printf(" MS 1 = %u MS 2 = %u MS 3 = %u MS 4 = %u MS 5 = %u MS 7 = %u\n",
00167 arm.readMicroswitch(0), arm.readMicroswitch(1), arm.readMicroswitch(2),
00168 arm.readMicroswitch(3), arm.readMicroswitch(4), arm.readMicroswitch(6));
00169 pc.printf("--------------------------------------------------------------------------------\n");
00170 pc.printf(" \n");
00171 pc.printf("--------------------------------------------------------------------------------\n");
00172 wait_ms(500);
00173 }
00174 break;
00175 case DEBUG:
00176 motor =0;
00177 while (!pc.readable())
00178 {
00179 pc.printf("hi = %u\n", motor);
00180 motor++;
00181 wait_ms(10);
00182 }
00183 break;
00184 }
00185 }
00186 }