Basic tank-style drive robot control firmware for Freescale FRDM-K64F. Controls motors on a Dual-Full-H-Bridge with EN, like DBH-1x series, from Bluetooth serial commands

Dependencies:   mbed

Committer:
Mr_What
Date:
Sun Aug 16 22:46:25 2015 +0000
Revision:
4:7620d21baef3
Parent:
3:502f90649834
seems to be running from app with reduced diagnostics

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mr_What 0:41ca27337c2b 1 // Aaron Birenboim, 26jul15
Mr_What 0:41ca27337c2b 2
Mr_What 0:41ca27337c2b 3 // Add some common Serial methods used in Arduino sketches
Mr_What 0:41ca27337c2b 4 //
Mr_What 0:41ca27337c2b 5 // Serial inherits from stream, for which I have no docuemntation.
Mr_What 0:41ca27337c2b 6 // examples seem to indicate that stream has (at least) printf
Mr_What 2:54d27fdcbe5c 7 //
Mr_What 2:54d27fdcbe5c 8 // I had trouble doing this with proper inheritance.
Mr_What 2:54d27fdcbe5c 9 // I gave up and just kept a refrence pointer
Mr_What 0:41ca27337c2b 10
Mr_What 0:41ca27337c2b 11 //#include "Serial.h"
Mr_What 0:41ca27337c2b 12
Mr_What 2:54d27fdcbe5c 13 #define EOLN "\r\n"
Mr_What 2:54d27fdcbe5c 14
Mr_What 2:54d27fdcbe5c 15 // singleton serial command byte buffer
Mr_What 4:7620d21baef3 16 #define CMDBUFLEN 64
Mr_What 2:54d27fdcbe5c 17 int cbuf[CMDBUFLEN];
Mr_What 2:54d27fdcbe5c 18 static int bufIn=0;
Mr_What 2:54d27fdcbe5c 19 static int bufOut=0;
Mr_What 2:54d27fdcbe5c 20 void gotChar() {
Mr_What 2:54d27fdcbe5c 21 int c = CmdSerial.getc();
Mr_What 2:54d27fdcbe5c 22 cbuf[bufIn % CMDBUFLEN]=c;
Mr_What 2:54d27fdcbe5c 23 bufIn++;
Mr_What 2:54d27fdcbe5c 24 // prevent byte count overflow
Mr_What 2:54d27fdcbe5c 25 // WARNING: if there is a race and this thread resets counts...
Mr_What 2:54d27fdcbe5c 26 // counts could look funny for a bit, but since bufOut
Mr_What 2:54d27fdcbe5c 27 // decrements before bufIn, one hopes that the glitch will not be problematic.
Mr_What 2:54d27fdcbe5c 28 // indication of char in the buffer remains correce, and one hopes that by the next
Mr_What 2:54d27fdcbe5c 29 // time they check, both counts are accurate.
Mr_What 4:7620d21baef3 30 if (bufIn > 401*CMDBUFLEN) {
Mr_What 4:7620d21baef3 31 bufOut -= 400*CMDBUFLEN;
Mr_What 4:7620d21baef3 32 bufIn -= 400*CMDBUFLEN;
Mr_What 2:54d27fdcbe5c 33 }
Mr_What 2:54d27fdcbe5c 34 }
Mr_What 2:54d27fdcbe5c 35
Mr_What 2:54d27fdcbe5c 36
Mr_What 2:54d27fdcbe5c 37
Mr_What 0:41ca27337c2b 38 class ASerial
Mr_What 0:41ca27337c2b 39 {
Mr_What 0:41ca27337c2b 40 public:
Mr_What 0:41ca27337c2b 41 Serial *_serial;
Mr_What 0:41ca27337c2b 42 ASerial(Serial &s) : _serial(&s)
Mr_What 0:41ca27337c2b 43 {
Mr_What 2:54d27fdcbe5c 44 //_serial->attach(&gotChar);
Mr_What 0:41ca27337c2b 45 }
Mr_What 0:41ca27337c2b 46
Mr_What 0:41ca27337c2b 47 void print(const char *s) { _serial->puts(s); }
Mr_What 0:41ca27337c2b 48 void print(const int i ) { _serial->printf("%d",i); }
Mr_What 0:41ca27337c2b 49 void print(const float f) { _serial->printf("%.3f",f); }
Mr_What 0:41ca27337c2b 50
Mr_What 2:54d27fdcbe5c 51 void println(const char *s) { print(s); _serial->puts(EOLN); }
Mr_What 2:54d27fdcbe5c 52 void println(const float f) { print(f); _serial->puts(EOLN); }
Mr_What 0:41ca27337c2b 53
Mr_What 0:41ca27337c2b 54 void baud(const int i) { _serial->baud(i); }
Mr_What 0:41ca27337c2b 55
Mr_What 2:54d27fdcbe5c 56 int cread()
Mr_What 0:41ca27337c2b 57 {
Mr_What 2:54d27fdcbe5c 58 /* didn't work. not sure why. something about this not being the pc serial?
Mr_What 2:54d27fdcbe5c 59 int n = _serial->readable();
Mr_What 2:54d27fdcbe5c 60 if (n)
Mr_What 0:41ca27337c2b 61 {
Mr_What 2:54d27fdcbe5c 62 CmdSerial.printf(" %d readable\t",n);
Mr_What 0:41ca27337c2b 63 char c = _serial->getc();
Mr_What 2:54d27fdcbe5c 64 CmdSerial.printf(" got '%c'(%d)\r\n",c,c);
Mr_What 0:41ca27337c2b 65 return((int)c);
Mr_What 0:41ca27337c2b 66 }
Mr_What 0:41ca27337c2b 67 return(-1);
Mr_What 2:54d27fdcbe5c 68 */
Mr_What 2:54d27fdcbe5c 69
Mr_What 2:54d27fdcbe5c 70 if(bufOut < bufIn) {
Mr_What 2:54d27fdcbe5c 71 int c = cbuf[bufOut % CMDBUFLEN];
Mr_What 2:54d27fdcbe5c 72 bufOut++;
Mr_What 4:7620d21baef3 73 //DiagSerial.printf("\r\n\t%d got %d(%c)\r\n",bufOut-1,c,c);
Mr_What 2:54d27fdcbe5c 74
Mr_What 2:54d27fdcbe5c 75
Mr_What 2:54d27fdcbe5c 76 int n = bufIn-bufOut;
Mr_What 2:54d27fdcbe5c 77 if ((n > CMDBUFLEN-3) && (n < 800*CMDBUFLEN)) {
Mr_What 4:7620d21baef3 78 DiagSerial.printf("\t\r\tCommand Buffer Overflow RESET!\r\n");
Mr_What 4:7620d21baef3 79 bufIn=bufOut=0;
Mr_What 2:54d27fdcbe5c 80 }
Mr_What 2:54d27fdcbe5c 81 return(c);
Mr_What 2:54d27fdcbe5c 82 } else { return(-1); }
Mr_What 0:41ca27337c2b 83 }
Mr_What 0:41ca27337c2b 84 };