This program contains a class that makes it easy for the mbed to communicate with the Mini SSC II or the Pololu Maestro in SSC compatibility mode. (they are servo/motor controllers)
Revision 3:5c664d91ba63, committed 2013-07-08
- Comitter:
- avbotz
- Date:
- Mon Jul 08 04:02:04 2013 +0000
- Parent:
- 2:e5458113c26b
- Commit message:
- Initial commit.;
Changed in this revision
--- a/.hgignore Sun Feb 26 02:03:17 2012 +0000 +++ b/.hgignore Mon Jul 08 04:02:04 2013 +0000 @@ -1,5 +1,7 @@ syntax: regexp +\.hgignore +\.msub \.meta -\.hgignore \.svn \.git +\.ctags
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jul 08 04:02:04 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912 \ No newline at end of file
--- a/mbed.lib Sun Feb 26 02:03:17 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/projects/libraries/svn/mbed/trunk@28 \ No newline at end of file
--- a/minissc.cpp Sun Feb 26 02:03:17 2012 +0000 +++ b/minissc.cpp Mon Jul 08 04:02:04 2013 +0000 @@ -29,8 +29,8 @@ } void MiniSSC2::send(int i_motor) { - // format: {sync byte, motor id, motor power, newline} - // example: {SSC_SYNC_BYTE, 2, 24, '\n'} sets motor 2 to power level 24 + // format: {sync byte, motor id, motor power} + // example: {SSC_SYNC_BYTE, 2, 24} sets motor 2 to power level 24 p_device->putc(SSC_SYNC_BYTE); p_device->putc((unsigned char)i_motor); p_device->putc(motors[i_motor]); @@ -55,25 +55,105 @@ ssc.send(); // is there a more efficient way to do this? } +void led1_on_cb() +{ + led1 = 1; +} + +void led1_off_cb() +{ + led1 = 0; +} + /* MAIN FUNCTION */ +// this code is very bad. please do not use it for anything serious. int main() { led1 = 1; wait_ms(500); //ssc_to.attach(&ssc_send_cb, 1); // run ssc_send_cb() at 10 Hz (Ticker) (increase if possible) PC.baud(115200); + PC.printf("\n\r\n\rHello.\n\r"); - PC.printf("\n\r\n\rHello."); + led1_on.attach(&led1_on_cb, 1.0f); + wait_ms(50); + led1_off.attach(&led1_off_cb, 1.0f); - while (true) { - for (int i = 0; i < 4; i++) { - unsigned char in = 127; - PC.printf("> "); - PC.scanf("%d", &in); - ssc.set(i, in); - PC.printf("\n\rSaved motor %d, power %d\n\r", i, (int)in); + ssc.set(1); + while (true) + { + led2 = led3 = led4 = 0; + led2 = 1; + ssc.send(); + /*PC.printf("Motors: l = left, r = right, f = forward, b = back.\n\r"); + PC.printf("Which motor? "); + int in = PC.getc(); + PC.printf("%c\n\r", in); + char motorID, motorData; + PC.printf("Setting motor "); + switch (in) + { + case 'r': + motorID = 0; + PC.printf("left.\n\r"); + break; + + case 'l': + motorID = 1; + PC.printf("right.\n\r"); + break; + + case 'f': + motorID = 2; + PC.printf("front.\n\r"); + break; + + case 'b': + motorID = 3; + PC.printf("back.\n\r"); + break; + + case 'q': + ssc.set(127); + ssc.send(); + break; + + default: + PC.printf("Invalid motor. Try again.\n\r"); + continue; + break; } - ssc.send(); - PC.printf("Sent all motors\n\r\n\r"); + led3 = 1; + PC.printf("Power (-100 to 100): "); + std::string powerRaw; + tryAgain: + powerRaw.clear(); + while (true) + { + in = PC.getc(); + PC.putc(in); + led4 = 1; + if (in == '\r') + { + PC.putc('\n'); + break; + } + powerRaw.push_back(in); + } + + if (sscanf(powerRaw.c_str(), "%d", &in) < 1 || in > 100 || in < -100) + { + printf("Invalid power. Try again: "); + goto tryAgain; + } + + in *= 1.27f; + in += 127; + + //PC.printf("Set motor %d to %d\n\r\n\r", motorID, in); + + ssc.set(motorID, in); + ssc.send(motorID); + + PC.printf("\n\r--------------------------------\n\r");*/ } } -// Don't delete this comment
--- a/minissc.h Sun Feb 26 02:03:17 2012 +0000 +++ b/minissc.h Mon Jul 08 04:02:04 2013 +0000 @@ -35,10 +35,11 @@ }; void ssc_send_cb(); +MiniSSC2 ssc(4, 9600, p13, p14); // 4 motors, baud for Mini SSC II is 9600, Mini SSC II is connected to pins 9 and 10 +Ticker ssc_to; -MiniSSC2 ssc(4, 9600, p9, p10); // 4 motors, baud for Mini SSC II is 9600, Mini SSC II is connected to pins 9 and 10 -Ticker ssc_to; +Ticker led1_on, led1_off; // debug Serial PC(USBTX, USBRX); // tx, rx -DigitalOut led1(LED1); \ No newline at end of file +DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); \ No newline at end of file