Matlab connection via the serial port, to control the robot from there (interactively)
MatlabComm.cpp@0:08d5f31c6ddc, 2016-11-01 (annotated)
- Committer:
- jbrouwer
- Date:
- Tue Nov 01 02:31:36 2016 +0000
- Revision:
- 0:08d5f31c6ddc
- Child:
- 1:40b82d961544
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbrouwer | 0:08d5f31c6ddc | 1 | #include "MatlabComm.h" |
jbrouwer | 0:08d5f31c6ddc | 2 | |
jbrouwer | 0:08d5f31c6ddc | 3 | MatlabComm::MatlabComm(PinName MotorDirectionPin, PinName MotorSpeedPin, PinName EncoderChannelA, PinName EncoderChannelB) : |
jbrouwer | 0:08d5f31c6ddc | 4 | M_SpeedPin(MotorSpeedPin), M_DirectionPin(MotorDirectionPin), M_Encoder(EncoderChannelA, EncoderChannelB, NC, 64, QEI::X4_ENCODING) |
jbrouwer | 0:08d5f31c6ddc | 5 | , pc(USBTX,USBRX) |
jbrouwer | 0:08d5f31c6ddc | 6 | { |
jbrouwer | 0:08d5f31c6ddc | 7 | |
jbrouwer | 0:08d5f31c6ddc | 8 | pc.baud(115200); |
jbrouwer | 0:08d5f31c6ddc | 9 | pc.printf("Hello\r\n"); |
jbrouwer | 0:08d5f31c6ddc | 10 | buffer_index=0; |
jbrouwer | 0:08d5f31c6ddc | 11 | } |
jbrouwer | 0:08d5f31c6ddc | 12 | |
jbrouwer | 0:08d5f31c6ddc | 13 | |
jbrouwer | 0:08d5f31c6ddc | 14 | // write data to the output. |
jbrouwer | 0:08d5f31c6ddc | 15 | void MatlabComm::write(){ |
jbrouwer | 0:08d5f31c6ddc | 16 | pc.printf("%i\r\n", M_Encoder.getPulses()+10); |
jbrouwer | 0:08d5f31c6ddc | 17 | }; |
jbrouwer | 0:08d5f31c6ddc | 18 | |
jbrouwer | 0:08d5f31c6ddc | 19 | void MatlabComm::flush_buffer(){ |
jbrouwer | 0:08d5f31c6ddc | 20 | for (int i=0; i < buffer_index; i++){ |
jbrouwer | 0:08d5f31c6ddc | 21 | char_buffer[i]=0; |
jbrouwer | 0:08d5f31c6ddc | 22 | } |
jbrouwer | 0:08d5f31c6ddc | 23 | buffer_index=0; |
jbrouwer | 0:08d5f31c6ddc | 24 | } |
jbrouwer | 0:08d5f31c6ddc | 25 | |
jbrouwer | 0:08d5f31c6ddc | 26 | void MatlabComm::read(){ |
jbrouwer | 0:08d5f31c6ddc | 27 | while (pc.readable()){ |
jbrouwer | 0:08d5f31c6ddc | 28 | char get = pc.getc(); |
jbrouwer | 0:08d5f31c6ddc | 29 | char_buffer[buffer_index] = get; |
jbrouwer | 0:08d5f31c6ddc | 30 | buffer_index++; |
jbrouwer | 0:08d5f31c6ddc | 31 | |
jbrouwer | 0:08d5f31c6ddc | 32 | // Check for a number. |
jbrouwer | 0:08d5f31c6ddc | 33 | if (get == '\n'){ |
jbrouwer | 0:08d5f31c6ddc | 34 | |
jbrouwer | 0:08d5f31c6ddc | 35 | double num; |
jbrouwer | 0:08d5f31c6ddc | 36 | int result; |
jbrouwer | 0:08d5f31c6ddc | 37 | result = sscanf(char_buffer,"%lf", &num); // Retrieve char from array; |
jbrouwer | 0:08d5f31c6ddc | 38 | if (result){ |
jbrouwer | 0:08d5f31c6ddc | 39 | M_DirectionPin = (int)(num > 0); |
jbrouwer | 0:08d5f31c6ddc | 40 | M_SpeedPin = abs(num); |
jbrouwer | 0:08d5f31c6ddc | 41 | flush_buffer(); |
jbrouwer | 0:08d5f31c6ddc | 42 | } |
jbrouwer | 0:08d5f31c6ddc | 43 | else { // other input, flush buffer. |
jbrouwer | 0:08d5f31c6ddc | 44 | flush_buffer(); |
jbrouwer | 0:08d5f31c6ddc | 45 | } |
jbrouwer | 0:08d5f31c6ddc | 46 | |
jbrouwer | 0:08d5f31c6ddc | 47 | } |
jbrouwer | 0:08d5f31c6ddc | 48 | |
jbrouwer | 0:08d5f31c6ddc | 49 | |
jbrouwer | 0:08d5f31c6ddc | 50 | if (buffer_index>buffer_max_index){ |
jbrouwer | 0:08d5f31c6ddc | 51 | flush_buffer(); |
jbrouwer | 0:08d5f31c6ddc | 52 | } |
jbrouwer | 0:08d5f31c6ddc | 53 | } |
jbrouwer | 0:08d5f31c6ddc | 54 | } |
jbrouwer | 0:08d5f31c6ddc | 55 | |
jbrouwer | 0:08d5f31c6ddc | 56 | void MatlabComm::Step(){ |
jbrouwer | 0:08d5f31c6ddc | 57 | write(); |
jbrouwer | 0:08d5f31c6ddc | 58 | read(); |
jbrouwer | 0:08d5f31c6ddc | 59 | } |