Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope Servo mbed QEI biquadFilter
main.cpp@5:3581013d4505, 2018-10-22 (annotated)
- Committer:
- s1725696
- Date:
- Mon Oct 22 09:33:18 2018 +0000
- Revision:
- 5:3581013d4505
- Parent:
- 4:8183e7b228f0
- Child:
- 6:f495a77c2c95
Counts are get, and potmeters controls the motor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s1725696 | 5:3581013d4505 | 1 | #include "mbed.h" // Use revision 119!! |
s1725696 | 5:3581013d4505 | 2 | #include "HIDScope.h" // For displaying data, select MBED - HID device, restart for every new code |
s1725696 | 5:3581013d4505 | 3 | #include "QEI.h" // For reading the encoder of the motors |
s1725696 | 0:cb8857cf3ea4 | 4 | |
s1725696 | 5:3581013d4505 | 5 | #define SERIAL_BAUD 115200 |
s1725696 | 5:3581013d4505 | 6 | |
s1725696 | 5:3581013d4505 | 7 | // In- en outputs |
s1725696 | 4:8183e7b228f0 | 8 | // ----------------------------------------------------------------------------- |
s1725696 | 4:8183e7b228f0 | 9 | |
s1725696 | 5:3581013d4505 | 10 | /* |
s1725696 | 0:cb8857cf3ea4 | 11 | // EMG related |
s1725696 | 5:3581013d4505 | 12 | AnalogIn emg1(); // EMG signal 1 |
s1725696 | 5:3581013d4505 | 13 | AnalogIn emg2(); // EMG signal 2 |
s1725696 | 5:3581013d4505 | 14 | // if needed |
s1725696 | 5:3581013d4505 | 15 | AnalogIn emg3(); // EMG signal 3 |
s1725696 | 5:3581013d4505 | 16 | AnalogIn emg4(); // EMG signal 4 |
s1725696 | 0:cb8857cf3ea4 | 17 | */ |
s1725696 | 0:cb8857cf3ea4 | 18 | |
s1725696 | 0:cb8857cf3ea4 | 19 | // Motor related |
s1725696 | 0:cb8857cf3ea4 | 20 | DigitalOut dirpin_1(D4); // direction of motor 1 |
s1725696 | 0:cb8857cf3ea4 | 21 | PwmOut pwmpin_1(D5); // PWM pin of motor 1 |
s1725696 | 5:3581013d4505 | 22 | DigitalOut dirpin_2(D7); // direction of motor 2 |
s1725696 | 5:3581013d4505 | 23 | PwmOut pwmpin_2(D6); // PWM pin of motor 2 |
s1725696 | 0:cb8857cf3ea4 | 24 | |
s1725696 | 0:cb8857cf3ea4 | 25 | // Extra stuff |
s1725696 | 0:cb8857cf3ea4 | 26 | // Like LED lights, buttons etc |
s1725696 | 5:3581013d4505 | 27 | /* |
s1725696 | 5:3581013d4505 | 28 | DigitalIn button_motorcal(); // button for motor calibration |
s1725696 | 5:3581013d4505 | 29 | DigitalIn button_emergency(); // button for emergency mode |
s1725696 | 5:3581013d4505 | 30 | DigitalIn button_start(); // button for start mode (from demo mode) |
s1725696 | 5:3581013d4505 | 31 | DigitalIn button_demo(); // button for demo mode |
s1725696 | 5:3581013d4505 | 32 | */ |
s1725696 | 4:8183e7b228f0 | 33 | DigitalIn led_red(LED_RED); // red led |
s1725696 | 4:8183e7b228f0 | 34 | DigitalIn led_green(LED_GREEN); // green led |
s1725696 | 4:8183e7b228f0 | 35 | DigitalIn led_blue(LED_BLUE); // blue led |
s1725696 | 5:3581013d4505 | 36 | AnalogIn pot_1(A1); // potmeter for simulating EMG input |
s1725696 | 5:3581013d4505 | 37 | |
s1725696 | 0:cb8857cf3ea4 | 38 | |
s1725696 | 0:cb8857cf3ea4 | 39 | // Other stuff |
s1725696 | 4:8183e7b228f0 | 40 | // ----------------------------------------------------------------------------- |
s1725696 | 0:cb8857cf3ea4 | 41 | // Define stuff like tickers etc |
s1725696 | 0:cb8857cf3ea4 | 42 | |
s1725696 | 5:3581013d4505 | 43 | // Ticker NAME; // Name a ticker, use each ticker only for 1 function! |
s1725696 | 0:cb8857cf3ea4 | 44 | HIDScope scope(2); // Number of channels in HIDScope |
s1725696 | 0:cb8857cf3ea4 | 45 | QEI Encoder(D13,D12,NC,64,QEI::X4_ENCODING); // Define the type of encoding: X4 encoding(default is X2) |
s1725696 | 5:3581013d4505 | 46 | Serial pc(USBTX,USBRX); |
s1725696 | 0:cb8857cf3ea4 | 47 | |
s1725696 | 0:cb8857cf3ea4 | 48 | // Variables |
s1725696 | 4:8183e7b228f0 | 49 | // ----------------------------------------------------------------------------- |
s1725696 | 0:cb8857cf3ea4 | 50 | // Define here all variables needed throughout the whole code |
s1725696 | 5:3581013d4505 | 51 | int counts; |
s1725696 | 0:cb8857cf3ea4 | 52 | |
s1725696 | 0:cb8857cf3ea4 | 53 | // Functions |
s1725696 | 4:8183e7b228f0 | 54 | // ----------------------------------------------------------------------------- |
s1725696 | 0:cb8857cf3ea4 | 55 | |
s1725696 | 4:8183e7b228f0 | 56 | // Encoder |
s1725696 | 4:8183e7b228f0 | 57 | // Getting encoder information from motors |
s1725696 | 4:8183e7b228f0 | 58 | int encoder() |
s1725696 | 4:8183e7b228f0 | 59 | { |
s1725696 | 5:3581013d4505 | 60 | int counts = Encoder.getPulses(); |
s1725696 | 4:8183e7b228f0 | 61 | return counts; |
s1725696 | 5:3581013d4505 | 62 | } |
s1725696 | 5:3581013d4505 | 63 | |
s1725696 | 5:3581013d4505 | 64 | // potmeter |
s1725696 | 5:3581013d4505 | 65 | float potmeter() |
s1725696 | 5:3581013d4505 | 66 | { |
s1725696 | 5:3581013d4505 | 67 | float out_1 = pot_1 * 2.0f; |
s1725696 | 5:3581013d4505 | 68 | float out_2 = out_1 - 1.0f; |
s1725696 | 5:3581013d4505 | 69 | |
s1725696 | 5:3581013d4505 | 70 | dirpin_1.write(out_2 < 0); |
s1725696 | 5:3581013d4505 | 71 | dirpin_2.write(out_2 < 0); |
s1725696 | 5:3581013d4505 | 72 | |
s1725696 | 5:3581013d4505 | 73 | pwmpin_1 = fabs (out_2); // Has to be positive value |
s1725696 | 5:3581013d4505 | 74 | pwmpin_2 = fabs (out_2); |
s1725696 | 5:3581013d4505 | 75 | |
s1725696 | 5:3581013d4505 | 76 | return out_2; |
s1725696 | 5:3581013d4505 | 77 | } |
s1725696 | 4:8183e7b228f0 | 78 | |
s1725696 | 0:cb8857cf3ea4 | 79 | // Motor calibration |
s1725696 | 0:cb8857cf3ea4 | 80 | // To calibrate the motor angle to some mechanical boundaries |
s1725696 | 4:8183e7b228f0 | 81 | // Kenneth mee bezig |
s1725696 | 0:cb8857cf3ea4 | 82 | |
s1725696 | 0:cb8857cf3ea4 | 83 | // EMG calibration |
s1725696 | 0:cb8857cf3ea4 | 84 | // To calibrate the EMG signal to some boundary values |
s1725696 | 0:cb8857cf3ea4 | 85 | |
s1725696 | 0:cb8857cf3ea4 | 86 | // Send EMG to HIDScope |
s1725696 | 4:8183e7b228f0 | 87 | void sample() // Attach this to a ticker! |
s1725696 | 0:cb8857cf3ea4 | 88 | { |
s1725696 | 5:3581013d4505 | 89 | scope.set(0, counts); // send counts to channel 1 (=0) |
s1725696 | 5:3581013d4505 | 90 | // scope.set(1, velocity_encoder() ); // sent EMG 2 to channel 2 (=1) |
s1725696 | 5:3581013d4505 | 91 | |
s1725696 | 1:1a8211e1f3f3 | 92 | // Ensure that enough channels are available (HIDScope scope(2)) |
s1725696 | 0:cb8857cf3ea4 | 93 | scope.send(); // Send all channels to the PC at once |
s1725696 | 0:cb8857cf3ea4 | 94 | } |
s1725696 | 0:cb8857cf3ea4 | 95 | |
s1725696 | 0:cb8857cf3ea4 | 96 | // Start |
s1725696 | 0:cb8857cf3ea4 | 97 | // To move the robot to the starting position: middle |
s1725696 | 4:8183e7b228f0 | 98 | void start() |
s1725696 | 4:8183e7b228f0 | 99 | { |
s1725696 | 4:8183e7b228f0 | 100 | // move to middle |
s1725696 | 4:8183e7b228f0 | 101 | } |
s1725696 | 0:cb8857cf3ea4 | 102 | |
s1725696 | 0:cb8857cf3ea4 | 103 | // Operating mode |
s1725696 | 0:cb8857cf3ea4 | 104 | // To control the robot with EMG signals |
s1725696 | 0:cb8857cf3ea4 | 105 | |
s1725696 | 0:cb8857cf3ea4 | 106 | // Processing EMG |
s1725696 | 0:cb8857cf3ea4 | 107 | // To process the raw EMG to a usable value, with filters etc |
s1725696 | 4:8183e7b228f0 | 108 | // Simon en Kees mee bezig |
s1725696 | 0:cb8857cf3ea4 | 109 | |
s1725696 | 0:cb8857cf3ea4 | 110 | // Demo mode |
s1725696 | 0:cb8857cf3ea4 | 111 | // To control the robot with a written code and write 'HELLO' |
s1725696 | 4:8183e7b228f0 | 112 | // Voor het laatst bewaren |
s1725696 | 0:cb8857cf3ea4 | 113 | |
s1725696 | 0:cb8857cf3ea4 | 114 | // Emergency mode |
s1725696 | 0:cb8857cf3ea4 | 115 | // To shut down the robot after an error etc |
s1725696 | 0:cb8857cf3ea4 | 116 | |
s1725696 | 0:cb8857cf3ea4 | 117 | // Main function |
s1725696 | 4:8183e7b228f0 | 118 | // ----------------------------------------------------------------------------- |
s1725696 | 0:cb8857cf3ea4 | 119 | int main() |
s1725696 | 0:cb8857cf3ea4 | 120 | { |
s1725696 | 0:cb8857cf3ea4 | 121 | pc.baud(115200); // For TeraTerm, the baudrate, set also in TeraTerm itself! |
s1725696 | 0:cb8857cf3ea4 | 122 | pc.printf("Start code\r\n"); // To check if the program starts |
s1725696 | 5:3581013d4505 | 123 | pwmpin_1.period_us(60); // Needed for PWM, not exactly known why |
s1725696 | 1:1a8211e1f3f3 | 124 | |
s1725696 | 1:1a8211e1f3f3 | 125 | while(true){ |
s1725696 | 5:3581013d4505 | 126 | |
s1725696 | 5:3581013d4505 | 127 | counts = encoder(); |
s1725696 | 5:3581013d4505 | 128 | float out_2 = potmeter(); |
s1725696 | 5:3581013d4505 | 129 | sample(); |
s1725696 | 5:3581013d4505 | 130 | |
s1725696 | 5:3581013d4505 | 131 | pc.printf("potmeter value = %f ", out_2); |
s1725696 | 5:3581013d4505 | 132 | pc.printf("counts = %i\r\n", counts); |
s1725696 | 5:3581013d4505 | 133 | |
s1725696 | 5:3581013d4505 | 134 | /* if (button_motorcal == true){ |
s1725696 | 4:8183e7b228f0 | 135 | // execute motor calibration |
s1725696 | 4:8183e7b228f0 | 136 | } |
s1725696 | 4:8183e7b228f0 | 137 | elseif { |
s1725696 | 4:8183e7b228f0 | 138 | // remain in waiting mode |
s1725696 | 4:8183e7b228f0 | 139 | break; |
s1725696 | 4:8183e7b228f0 | 140 | } |
s1725696 | 5:3581013d4505 | 141 | */ |
s1725696 | 5:3581013d4505 | 142 | |
s1725696 | 5:3581013d4505 | 143 | wait(0.01); // loop the while loop every 0.01 seconds |
s1725696 | 5:3581013d4505 | 144 | } |
s1725696 | 5:3581013d4505 | 145 | |
s1725696 | 0:cb8857cf3ea4 | 146 | } |