Matlab connection via the serial port, to control the robot from there (interactively)
MatlabComm.h@3:4fd668c1dbf5, 2016-11-01 (annotated)
- Committer:
- jbrouwer
- Date:
- Tue Nov 01 02:36:29 2016 +0000
- Revision:
- 3:4fd668c1dbf5
- Parent:
- 0:08d5f31c6ddc
Added example code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbrouwer | 0:08d5f31c6ddc | 1 | #include "QEI.h" |
jbrouwer | 0:08d5f31c6ddc | 2 | #include "MODSERIAL.h" |
jbrouwer | 0:08d5f31c6ddc | 3 | #include "stdio.h" //sscanf |
jbrouwer | 0:08d5f31c6ddc | 4 | #include "mbed.h" |
jbrouwer | 0:08d5f31c6ddc | 5 | #include "math.h" // abs |
jbrouwer | 0:08d5f31c6ddc | 6 | |
jbrouwer | 0:08d5f31c6ddc | 7 | // Serial communications with Matlab, to control the robot and make a design. |
jbrouwer | 0:08d5f31c6ddc | 8 | // Communications sent: |
jbrouwer | 0:08d5f31c6ddc | 9 | // %i; Pulses sent, followed by a \r\n; |
jbrouwer | 0:08d5f31c6ddc | 10 | // This will be sent on every call of MatlabComm. |
jbrouwer | 0:08d5f31c6ddc | 11 | // In case of data, MatlabComm will read the Serial input buffer until it is empty, or until it has read a number |
jbrouwer | 0:08d5f31c6ddc | 12 | |
jbrouwer | 0:08d5f31c6ddc | 13 | class MatlabComm { |
jbrouwer | 0:08d5f31c6ddc | 14 | public: |
jbrouwer | 0:08d5f31c6ddc | 15 | MatlabComm(PinName MotorDirectionPin, PinName MotorSpeedPin, PinName EncoderChannelA, PinName EncoderChannelB); |
jbrouwer | 0:08d5f31c6ddc | 16 | void Step(); |
jbrouwer | 0:08d5f31c6ddc | 17 | private: |
jbrouwer | 0:08d5f31c6ddc | 18 | PwmOut M_SpeedPin; |
jbrouwer | 0:08d5f31c6ddc | 19 | DigitalOut M_DirectionPin; |
jbrouwer | 0:08d5f31c6ddc | 20 | QEI M_Encoder; |
jbrouwer | 0:08d5f31c6ddc | 21 | MODSERIAL pc; |
jbrouwer | 0:08d5f31c6ddc | 22 | void read(); |
jbrouwer | 0:08d5f31c6ddc | 23 | void write(); |
jbrouwer | 0:08d5f31c6ddc | 24 | void flush_buffer(); |
jbrouwer | 0:08d5f31c6ddc | 25 | static const int buffer_max_index=29; |
jbrouwer | 0:08d5f31c6ddc | 26 | char char_buffer[30]; |
jbrouwer | 0:08d5f31c6ddc | 27 | int buffer_index; |
jbrouwer | 0:08d5f31c6ddc | 28 | }; |