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.
Fork of mbed-os-example-mbed5-blinky by
main.cpp@43:5123f24e0b2c, 2017-07-07 (annotated)
- Committer:
- gunarthon
- Date:
- Fri Jul 07 00:16:14 2017 +0000
- Revision:
- 43:5123f24e0b2c
- Parent:
- 42:b69538bba4f9
final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
gunarthon | 38:b760c09b311c | 2 | #include "Queue.h" |
gunarthon | 38:b760c09b311c | 3 | #include "TextLCD.h" |
gunarthon | 38:b760c09b311c | 4 | #include "pid.h" |
gunarthon | 39:e4f5710b2f31 | 5 | #include <Serial.h> |
gunarthon | 42:b69538bba4f9 | 6 | #include "EthernetInterface.h" |
Jonathan Austin |
0:2757d7abb7d9 | 7 | |
gunarthon | 38:b760c09b311c | 8 | //definitions |
gunarthon | 38:b760c09b311c | 9 | typedef struct { |
gunarthon | 40:19d51f6e6800 | 10 | double input; |
gunarthon | 38:b760c09b311c | 11 | } message_t; |
gunarthon | 38:b760c09b311c | 12 | |
gunarthon | 42:b69538bba4f9 | 13 | bool useUDP = false; |
gunarthon | 40:19d51f6e6800 | 14 | const int heightResolution = 1000; |
gunarthon | 39:e4f5710b2f31 | 15 | const int BAUDRATE = 115200; |
gunarthon | 42:b69538bba4f9 | 16 | const char* host_address = "10.1.1.101"; |
gunarthon | 42:b69538bba4f9 | 17 | const int host_port = 7; |
gunarthon | 40:19d51f6e6800 | 18 | const int cameraFPS = 60; |
gunarthon | 40:19d51f6e6800 | 19 | const int displayRefreshTime = 200; |
gunarthon | 38:b760c09b311c | 20 | const double setPoint = 0.5; |
gunarthon | 40:19d51f6e6800 | 21 | const double Kp = 0.02; |
gunarthon | 40:19d51f6e6800 | 22 | const double Ki = 0; |
gunarthon | 40:19d51f6e6800 | 23 | const double Kd = 0; |
gunarthon | 41:3bc2a3885b9d | 24 | const double configStep = 0.001; |
gunarthon | 41:3bc2a3885b9d | 25 | const double configFastStep = configStep*10; |
gunarthon | 38:b760c09b311c | 26 | |
gunarthon | 40:19d51f6e6800 | 27 | const int simulationSeconds = 10; |
gunarthon | 40:19d51f6e6800 | 28 | |
gunarthon | 40:19d51f6e6800 | 29 | enum {btnRIGHT, btnUP, btnDOWN, btnLEFT, btnSELECT, btnNONE, btnSIZE}; |
gunarthon | 40:19d51f6e6800 | 30 | enum {menuSETPOINT, menuPID, menuSIMULATE, menuSIZE}; |
gunarthon | 40:19d51f6e6800 | 31 | enum {parameterKP, parameterKI, parameterKD, parameterSIZE}; |
gunarthon | 40:19d51f6e6800 | 32 | enum {simulSTEP, simulRAMP, simulNONE, simulSIZE}; |
Jonathan Austin |
0:2757d7abb7d9 | 33 | |
gunarthon | 38:b760c09b311c | 34 | //Pins |
gunarthon | 38:b760c09b311c | 35 | PwmOut outPin(D3); |
gunarthon | 43:5123f24e0b2c | 36 | DigitalOut led1(LED1); |
gunarthon | 40:19d51f6e6800 | 37 | //DigitalOut led2(LED2); |
gunarthon | 38:b760c09b311c | 38 | DigitalOut led3(LED3); |
gunarthon | 38:b760c09b311c | 39 | AnalogIn buttons(A0); |
gunarthon | 38:b760c09b311c | 40 | |
gunarthon | 38:b760c09b311c | 41 | //Threads |
gunarthon | 41:3bc2a3885b9d | 42 | Thread lcdThread; |
gunarthon | 38:b760c09b311c | 43 | Thread pidThread; |
gunarthon | 42:b69538bba4f9 | 44 | Thread serialThread; |
gunarthon | 38:b760c09b311c | 45 | Thread hmiThread; |
gunarthon | 40:19d51f6e6800 | 46 | Thread setPointThread; |
gunarthon | 42:b69538bba4f9 | 47 | Thread udpThread; |
gunarthon | 38:b760c09b311c | 48 | |
gunarthon | 38:b760c09b311c | 49 | //Global variables |
gunarthon | 38:b760c09b311c | 50 | TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7 |
gunarthon | 38:b760c09b311c | 51 | Pid* pidController; |
gunarthon | 38:b760c09b311c | 52 | MemoryPool<message_t, 16> mpool; |
gunarthon | 38:b760c09b311c | 53 | Queue<message_t,16> messageQueue; |
gunarthon | 38:b760c09b311c | 54 | |
gunarthon | 40:19d51f6e6800 | 55 | //Menu settings |
gunarthon | 40:19d51f6e6800 | 56 | int menu = menuSETPOINT; |
gunarthon | 40:19d51f6e6800 | 57 | int parameter = parameterKP; |
gunarthon | 40:19d51f6e6800 | 58 | int simul = simulNONE; |
gunarthon | 41:3bc2a3885b9d | 59 | bool setPointSimul = false; |
gunarthon | 40:19d51f6e6800 | 60 | |
gunarthon | 42:b69538bba4f9 | 61 | |
gunarthon | 42:b69538bba4f9 | 62 | // Network interface |
gunarthon | 42:b69538bba4f9 | 63 | EthernetInterface net; |
gunarthon | 39:e4f5710b2f31 | 64 | |
gunarthon | 38:b760c09b311c | 65 | //=============================Thread Methodes================================== |
gunarthon | 40:19d51f6e6800 | 66 | void setPointMethode(void) |
gunarthon | 40:19d51f6e6800 | 67 | { |
gunarthon | 41:3bc2a3885b9d | 68 | while(1) //toggle every 20 seconds between 2 setPoint values |
gunarthon | 40:19d51f6e6800 | 69 | { |
gunarthon | 41:3bc2a3885b9d | 70 | while(!setPointSimul) Thread::wait(2000); |
gunarthon | 41:3bc2a3885b9d | 71 | |
gunarthon | 41:3bc2a3885b9d | 72 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 73 | { |
gunarthon | 41:3bc2a3885b9d | 74 | pidController->setSetPoint(0.2); |
gunarthon | 41:3bc2a3885b9d | 75 | Thread::wait(20000); |
gunarthon | 41:3bc2a3885b9d | 76 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 77 | { |
gunarthon | 41:3bc2a3885b9d | 78 | pidController->setSetPoint(0.8); |
gunarthon | 41:3bc2a3885b9d | 79 | Thread::wait(20000); |
gunarthon | 41:3bc2a3885b9d | 80 | } |
gunarthon | 41:3bc2a3885b9d | 81 | } |
gunarthon | 40:19d51f6e6800 | 82 | } |
gunarthon | 40:19d51f6e6800 | 83 | } |
gunarthon | 41:3bc2a3885b9d | 84 | void LcdMethode(void) |
gunarthon | 38:b760c09b311c | 85 | { |
gunarthon | 38:b760c09b311c | 86 | while(true) |
gunarthon | 38:b760c09b311c | 87 | { |
gunarthon | 40:19d51f6e6800 | 88 | double lastPwm = pidController->getLastPwm(); |
gunarthon | 40:19d51f6e6800 | 89 | double lastInput = pidController->getLastInput(); |
gunarthon | 40:19d51f6e6800 | 90 | |
gunarthon | 40:19d51f6e6800 | 91 | if(menu == menuSETPOINT) |
gunarthon | 40:19d51f6e6800 | 92 | { |
gunarthon | 40:19d51f6e6800 | 93 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 94 | lcd.printf("set in out"); |
gunarthon | 40:19d51f6e6800 | 95 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 96 | lcd.printf("%d", int(1000*pidController->getSetPoint())); |
gunarthon | 40:19d51f6e6800 | 97 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 98 | lcd.printf("%d", int(1000*lastInput)); |
gunarthon | 40:19d51f6e6800 | 99 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 100 | lcd.printf("%d", int(1000*lastPwm)); |
gunarthon | 40:19d51f6e6800 | 101 | } |
gunarthon | 40:19d51f6e6800 | 102 | else if(menu == menuPID) |
gunarthon | 40:19d51f6e6800 | 103 | { |
gunarthon | 40:19d51f6e6800 | 104 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 105 | lcd.printf(" Kp Ki Kd"); |
gunarthon | 40:19d51f6e6800 | 106 | lcd.locate(6*parameter, 0); |
gunarthon | 40:19d51f6e6800 | 107 | lcd.printf("*"); |
gunarthon | 40:19d51f6e6800 | 108 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 109 | lcd.printf("%d", int(1000*pidController->getKp())); |
gunarthon | 40:19d51f6e6800 | 110 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 111 | lcd.printf("%d", int(1000*pidController->getKi())); |
gunarthon | 40:19d51f6e6800 | 112 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 113 | lcd.printf("%d", int(1000*pidController->getKd())); |
gunarthon | 40:19d51f6e6800 | 114 | } |
gunarthon | 40:19d51f6e6800 | 115 | else if(menu == menuSIMULATE) |
gunarthon | 40:19d51f6e6800 | 116 | { |
gunarthon | 40:19d51f6e6800 | 117 | lcd.cls(); |
gunarthon | 40:19d51f6e6800 | 118 | if(simul == simulNONE) |
gunarthon | 41:3bc2a3885b9d | 119 | { |
gunarthon | 41:3bc2a3885b9d | 120 | if(setPointSimul) |
gunarthon | 41:3bc2a3885b9d | 121 | lcd.printf("u:STEP * d:RAMP"); |
gunarthon | 41:3bc2a3885b9d | 122 | else |
gunarthon | 41:3bc2a3885b9d | 123 | lcd.printf("u:STEP _ d:RAMP"); |
gunarthon | 41:3bc2a3885b9d | 124 | } |
gunarthon | 40:19d51f6e6800 | 125 | else |
gunarthon | 40:19d51f6e6800 | 126 | lcd.printf("set $in$ out"); |
gunarthon | 40:19d51f6e6800 | 127 | lcd.locate(0,1); |
gunarthon | 40:19d51f6e6800 | 128 | lcd.printf("%d", int(1000*pidController->getSetPoint())); |
gunarthon | 40:19d51f6e6800 | 129 | lcd.locate(6,1); |
gunarthon | 40:19d51f6e6800 | 130 | lcd.printf("%d", int(1000*lastInput)); |
gunarthon | 40:19d51f6e6800 | 131 | lcd.locate(12,1); |
gunarthon | 40:19d51f6e6800 | 132 | lcd.printf("%d", int(1000*lastPwm)); |
gunarthon | 40:19d51f6e6800 | 133 | } |
gunarthon | 40:19d51f6e6800 | 134 | Thread::wait(displayRefreshTime); |
Jonathan Austin |
0:2757d7abb7d9 | 135 | } |
Jonathan Austin |
0:2757d7abb7d9 | 136 | } |
Jonathan Austin |
1:846c97078558 | 137 | |
gunarthon | 38:b760c09b311c | 138 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 139 | |
gunarthon | 38:b760c09b311c | 140 | void PidMethode(void) |
gunarthon | 38:b760c09b311c | 141 | { |
gunarthon | 38:b760c09b311c | 142 | while(true) |
gunarthon | 38:b760c09b311c | 143 | { |
gunarthon | 38:b760c09b311c | 144 | osEvent ev = messageQueue.get(osWaitForever); |
gunarthon | 38:b760c09b311c | 145 | if (ev.status == osEventMessage) |
gunarthon | 38:b760c09b311c | 146 | { |
gunarthon | 38:b760c09b311c | 147 | message_t *message = (message_t*)ev.value.p; |
gunarthon | 38:b760c09b311c | 148 | |
gunarthon | 40:19d51f6e6800 | 149 | double input = message->input; |
gunarthon | 38:b760c09b311c | 150 | |
gunarthon | 38:b760c09b311c | 151 | double newPwm = pidController->getPwm(input); |
gunarthon | 38:b760c09b311c | 152 | |
gunarthon | 38:b760c09b311c | 153 | outPin.write(newPwm); |
gunarthon | 38:b760c09b311c | 154 | |
gunarthon | 38:b760c09b311c | 155 | mpool.free(message); |
gunarthon | 38:b760c09b311c | 156 | } |
gunarthon | 38:b760c09b311c | 157 | } |
gunarthon | 38:b760c09b311c | 158 | } |
gunarthon | 38:b760c09b311c | 159 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 160 | |
gunarthon | 42:b69538bba4f9 | 161 | void SerialMethode(void) |
gunarthon | 38:b760c09b311c | 162 | { |
gunarthon | 39:e4f5710b2f31 | 163 | Serial serial(USBTX, USBRX); |
gunarthon | 39:e4f5710b2f31 | 164 | serial.baud(BAUDRATE); |
gunarthon | 39:e4f5710b2f31 | 165 | serial.format(8, SerialBase::None, 1); |
gunarthon | 40:19d51f6e6800 | 166 | double input = 0; |
gunarthon | 39:e4f5710b2f31 | 167 | |
gunarthon | 38:b760c09b311c | 168 | while(true) |
gunarthon | 38:b760c09b311c | 169 | { |
gunarthon | 40:19d51f6e6800 | 170 | input = double(serial.getc()+(serial.getc()<<8)) / heightResolution; |
gunarthon | 38:b760c09b311c | 171 | message_t *message = mpool.alloc(); |
gunarthon | 38:b760c09b311c | 172 | message->input = input; |
gunarthon | 38:b760c09b311c | 173 | messageQueue.put(message); |
gunarthon | 38:b760c09b311c | 174 | } |
gunarthon | 38:b760c09b311c | 175 | } |
gunarthon | 42:b69538bba4f9 | 176 | //------------------------------------------------------------------------------ |
gunarthon | 42:b69538bba4f9 | 177 | |
gunarthon | 42:b69538bba4f9 | 178 | void UdpMethode(void) |
gunarthon | 42:b69538bba4f9 | 179 | { |
gunarthon | 42:b69538bba4f9 | 180 | UDPSocket socket(&net); |
gunarthon | 42:b69538bba4f9 | 181 | socket.bind(host_port); |
gunarthon | 42:b69538bba4f9 | 182 | SocketAddress socketAddress; |
gunarthon | 42:b69538bba4f9 | 183 | socket.set_blocking(true); |
gunarthon | 42:b69538bba4f9 | 184 | uint16_t packet = 0; |
gunarthon | 42:b69538bba4f9 | 185 | double input = 0; |
gunarthon | 42:b69538bba4f9 | 186 | while(true) |
gunarthon | 42:b69538bba4f9 | 187 | { |
gunarthon | 42:b69538bba4f9 | 188 | //socket.sendto(host_address, host_port, (const void*)input, sizeof(input)); |
gunarthon | 42:b69538bba4f9 | 189 | //input += 1; |
gunarthon | 42:b69538bba4f9 | 190 | socket.recvfrom(&socketAddress, &packet, sizeof(packet)); |
gunarthon | 42:b69538bba4f9 | 191 | |
gunarthon | 42:b69538bba4f9 | 192 | input = double(packet) / heightResolution; |
gunarthon | 42:b69538bba4f9 | 193 | message_t *message = mpool.alloc(); |
gunarthon | 42:b69538bba4f9 | 194 | message->input = input; |
gunarthon | 42:b69538bba4f9 | 195 | messageQueue.put(message); |
gunarthon | 42:b69538bba4f9 | 196 | Thread::wait(300); |
gunarthon | 42:b69538bba4f9 | 197 | } |
gunarthon | 42:b69538bba4f9 | 198 | } |
gunarthon | 42:b69538bba4f9 | 199 | |
gunarthon | 42:b69538bba4f9 | 200 | //------------------------------------------------------------------------------ |
gunarthon | 38:b760c09b311c | 201 | |
gunarthon | 40:19d51f6e6800 | 202 | unsigned readButtons() |
gunarthon | 38:b760c09b311c | 203 | { |
gunarthon | 38:b760c09b311c | 204 | double buttonsValue = buttons.read(); |
gunarthon | 38:b760c09b311c | 205 | |
gunarthon | 38:b760c09b311c | 206 | unsigned button = btnNONE; |
gunarthon | 38:b760c09b311c | 207 | if(buttonsValue < 0.08) //0.000 |
gunarthon | 38:b760c09b311c | 208 | button = btnRIGHT; |
gunarthon | 38:b760c09b311c | 209 | else if(buttonsValue < 0.28) //0.170 |
gunarthon | 38:b760c09b311c | 210 | button = btnUP; |
gunarthon | 38:b760c09b311c | 211 | else if(buttonsValue < 0.51) //0.397 |
gunarthon | 38:b760c09b311c | 212 | button = btnDOWN; |
gunarthon | 38:b760c09b311c | 213 | else if(buttonsValue < 0.78) //0.621 |
gunarthon | 38:b760c09b311c | 214 | button = btnLEFT; |
gunarthon | 38:b760c09b311c | 215 | else if(buttonsValue < 0.97) //0.936 |
gunarthon | 38:b760c09b311c | 216 | button = btnSELECT; |
gunarthon | 38:b760c09b311c | 217 | else //1.000 |
gunarthon | 38:b760c09b311c | 218 | button = btnNONE; |
gunarthon | 38:b760c09b311c | 219 | |
gunarthon | 40:19d51f6e6800 | 220 | return button; |
gunarthon | 40:19d51f6e6800 | 221 | |
gunarthon | 40:19d51f6e6800 | 222 | } |
gunarthon | 40:19d51f6e6800 | 223 | //------------------------------------------------------------------------------ |
gunarthon | 40:19d51f6e6800 | 224 | //human-machine interface |
gunarthon | 40:19d51f6e6800 | 225 | void hmiMethode(void) |
gunarthon | 40:19d51f6e6800 | 226 | { |
gunarthon | 40:19d51f6e6800 | 227 | |
gunarthon | 40:19d51f6e6800 | 228 | unsigned button = btnNONE; |
gunarthon | 41:3bc2a3885b9d | 229 | bool fastChange = false; |
gunarthon | 40:19d51f6e6800 | 230 | while(true) |
gunarthon | 40:19d51f6e6800 | 231 | { |
gunarthon | 38:b760c09b311c | 232 | |
gunarthon | 41:3bc2a3885b9d | 233 | button = readButtons(); |
gunarthon | 41:3bc2a3885b9d | 234 | while(button == btnNONE) |
gunarthon | 41:3bc2a3885b9d | 235 | { |
gunarthon | 40:19d51f6e6800 | 236 | button = readButtons(); |
gunarthon | 41:3bc2a3885b9d | 237 | Thread::wait(10); |
gunarthon | 41:3bc2a3885b9d | 238 | fastChange = false; |
gunarthon | 41:3bc2a3885b9d | 239 | } |
gunarthon | 40:19d51f6e6800 | 240 | |
gunarthon | 38:b760c09b311c | 241 | double prevSetPoint = pidController->getSetPoint(); |
gunarthon | 38:b760c09b311c | 242 | double newSetPoint = prevSetPoint; |
gunarthon | 40:19d51f6e6800 | 243 | |
gunarthon | 40:19d51f6e6800 | 244 | if(button == btnSELECT) |
gunarthon | 40:19d51f6e6800 | 245 | { |
gunarthon | 40:19d51f6e6800 | 246 | menu = (menu+1) % menuSIZE; |
gunarthon | 40:19d51f6e6800 | 247 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 248 | } |
gunarthon | 40:19d51f6e6800 | 249 | else if(menu == menuSETPOINT) |
gunarthon | 40:19d51f6e6800 | 250 | { |
gunarthon | 40:19d51f6e6800 | 251 | if(button == btnUP) |
gunarthon | 41:3bc2a3885b9d | 252 | newSetPoint += fastChange ? configFastStep : configStep; |
gunarthon | 40:19d51f6e6800 | 253 | else if(button == btnDOWN) |
gunarthon | 41:3bc2a3885b9d | 254 | newSetPoint -= fastChange ? configFastStep : configStep; |
gunarthon | 40:19d51f6e6800 | 255 | else if(button == btnLEFT) |
gunarthon | 41:3bc2a3885b9d | 256 | newSetPoint -= fastChange ? 10*configFastStep : 10*configStep; |
gunarthon | 40:19d51f6e6800 | 257 | else if(button == btnRIGHT) |
gunarthon | 41:3bc2a3885b9d | 258 | newSetPoint += fastChange ? 10*configFastStep : 10*configStep; |
gunarthon | 40:19d51f6e6800 | 259 | |
gunarthon | 40:19d51f6e6800 | 260 | pidController->setSetPoint(newSetPoint); |
gunarthon | 40:19d51f6e6800 | 261 | } |
gunarthon | 40:19d51f6e6800 | 262 | else if(menu == menuPID) |
gunarthon | 40:19d51f6e6800 | 263 | { |
gunarthon | 40:19d51f6e6800 | 264 | if(button == btnUP) |
gunarthon | 40:19d51f6e6800 | 265 | { |
gunarthon | 40:19d51f6e6800 | 266 | if(parameter == parameterKP) |
gunarthon | 41:3bc2a3885b9d | 267 | pidController->addKp(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 268 | else if(parameter == parameterKI) |
gunarthon | 41:3bc2a3885b9d | 269 | pidController->addKi(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 270 | else if(parameter == parameterKD) |
gunarthon | 41:3bc2a3885b9d | 271 | pidController->addKd(fastChange ? configFastStep : configStep); |
gunarthon | 40:19d51f6e6800 | 272 | } |
gunarthon | 40:19d51f6e6800 | 273 | else if(button == btnDOWN) |
gunarthon | 40:19d51f6e6800 | 274 | { |
gunarthon | 40:19d51f6e6800 | 275 | if(parameter == parameterKP) |
gunarthon | 41:3bc2a3885b9d | 276 | pidController->addKp(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 277 | else if(parameter == parameterKI) |
gunarthon | 41:3bc2a3885b9d | 278 | pidController->addKi(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 279 | else if(parameter == parameterKD) |
gunarthon | 41:3bc2a3885b9d | 280 | pidController->addKd(fastChange ? -configFastStep : -configStep); |
gunarthon | 40:19d51f6e6800 | 281 | } |
gunarthon | 40:19d51f6e6800 | 282 | else if(button == btnLEFT) |
gunarthon | 40:19d51f6e6800 | 283 | parameter = (parameter+1) % parameterSIZE; |
gunarthon | 40:19d51f6e6800 | 284 | else if(button == btnRIGHT) |
gunarthon | 40:19d51f6e6800 | 285 | pidController->setParameters(Kp, Ki, Kd); |
gunarthon | 40:19d51f6e6800 | 286 | } |
gunarthon | 40:19d51f6e6800 | 287 | |
gunarthon | 40:19d51f6e6800 | 288 | else if(menu == menuSIMULATE) |
gunarthon | 40:19d51f6e6800 | 289 | { |
gunarthon | 40:19d51f6e6800 | 290 | if(button == btnUP) //step |
gunarthon | 40:19d51f6e6800 | 291 | { |
gunarthon | 40:19d51f6e6800 | 292 | simul = simulSTEP; |
gunarthon | 40:19d51f6e6800 | 293 | for(unsigned i = 0; i < cameraFPS * simulationSeconds; i++) |
gunarthon | 40:19d51f6e6800 | 294 | { |
gunarthon | 40:19d51f6e6800 | 295 | double input = i < cameraFPS * simulationSeconds / 2 ? 0 : 1; |
gunarthon | 40:19d51f6e6800 | 296 | message_t *message = mpool.alloc(); |
gunarthon | 40:19d51f6e6800 | 297 | message->input = input; |
gunarthon | 40:19d51f6e6800 | 298 | messageQueue.put(message); |
gunarthon | 40:19d51f6e6800 | 299 | |
gunarthon | 40:19d51f6e6800 | 300 | Thread::wait(1000/cameraFPS); |
gunarthon | 40:19d51f6e6800 | 301 | } |
gunarthon | 40:19d51f6e6800 | 302 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 303 | } |
gunarthon | 40:19d51f6e6800 | 304 | else if(button == btnDOWN) //ramp |
gunarthon | 40:19d51f6e6800 | 305 | { |
gunarthon | 40:19d51f6e6800 | 306 | simul = simulRAMP; |
gunarthon | 40:19d51f6e6800 | 307 | for(unsigned i = 0; i < cameraFPS * simulationSeconds; i++) |
gunarthon | 40:19d51f6e6800 | 308 | { |
gunarthon | 40:19d51f6e6800 | 309 | double input = double(i) / ((cameraFPS * simulationSeconds)-1); |
gunarthon | 40:19d51f6e6800 | 310 | message_t *message = mpool.alloc(); |
gunarthon | 40:19d51f6e6800 | 311 | message->input = input; |
gunarthon | 40:19d51f6e6800 | 312 | messageQueue.put(message); |
gunarthon | 40:19d51f6e6800 | 313 | |
gunarthon | 40:19d51f6e6800 | 314 | Thread::wait(1000/cameraFPS); |
gunarthon | 40:19d51f6e6800 | 315 | } |
gunarthon | 40:19d51f6e6800 | 316 | simul = simulNONE; |
gunarthon | 40:19d51f6e6800 | 317 | } |
gunarthon | 41:3bc2a3885b9d | 318 | else if(button == btnLEFT) |
gunarthon | 41:3bc2a3885b9d | 319 | setPointSimul = true; |
gunarthon | 40:19d51f6e6800 | 320 | else if(button == btnRIGHT) |
gunarthon | 41:3bc2a3885b9d | 321 | setPointSimul = false; |
gunarthon | 38:b760c09b311c | 322 | |
gunarthon | 40:19d51f6e6800 | 323 | } |
gunarthon | 41:3bc2a3885b9d | 324 | unsigned repeatCount = 0; |
gunarthon | 41:3bc2a3885b9d | 325 | while(button != btnNONE && !fastChange) |
gunarthon | 40:19d51f6e6800 | 326 | { |
gunarthon | 41:3bc2a3885b9d | 327 | repeatCount++; |
gunarthon | 40:19d51f6e6800 | 328 | button = readButtons(); |
gunarthon | 40:19d51f6e6800 | 329 | Thread::wait(10); |
gunarthon | 41:3bc2a3885b9d | 330 | if(repeatCount > 50) |
gunarthon | 41:3bc2a3885b9d | 331 | fastChange = true; |
gunarthon | 41:3bc2a3885b9d | 332 | } |
gunarthon | 38:b760c09b311c | 333 | Thread::wait(100); |
gunarthon | 38:b760c09b311c | 334 | } |
gunarthon | 38:b760c09b311c | 335 | } |
gunarthon | 38:b760c09b311c | 336 | |
gunarthon | 38:b760c09b311c | 337 | //=============================Main Thread====================================== |
gunarthon | 38:b760c09b311c | 338 | // main() runs in its own thread in the OS |
gunarthon | 38:b760c09b311c | 339 | |
gunarthon | 38:b760c09b311c | 340 | int main() { |
gunarthon | 38:b760c09b311c | 341 | |
gunarthon | 38:b760c09b311c | 342 | //led1.period(4.0f); // 4 second period |
gunarthon | 38:b760c09b311c | 343 | |
gunarthon | 38:b760c09b311c | 344 | lcd.cls(); |
gunarthon | 38:b760c09b311c | 345 | lcd.printf("HELLO"); |
gunarthon | 38:b760c09b311c | 346 | |
gunarthon | 42:b69538bba4f9 | 347 | Thread::wait(2000); |
gunarthon | 42:b69538bba4f9 | 348 | if(readButtons() == btnDOWN) |
gunarthon | 42:b69538bba4f9 | 349 | useUDP = true; |
gunarthon | 42:b69538bba4f9 | 350 | |
gunarthon | 42:b69538bba4f9 | 351 | if(useUDP) |
gunarthon | 42:b69538bba4f9 | 352 | { |
gunarthon | 42:b69538bba4f9 | 353 | lcd.cls(); |
gunarthon | 42:b69538bba4f9 | 354 | lcd.printf("Connecting..."); |
gunarthon | 42:b69538bba4f9 | 355 | net.connect(); |
gunarthon | 42:b69538bba4f9 | 356 | |
gunarthon | 42:b69538bba4f9 | 357 | // Show the network address |
gunarthon | 42:b69538bba4f9 | 358 | const char *ip = net.get_ip_address(); |
gunarthon | 42:b69538bba4f9 | 359 | lcd.cls(); |
gunarthon | 42:b69538bba4f9 | 360 | lcd.printf("%s", ip ? ip : "No IP"); |
gunarthon | 42:b69538bba4f9 | 361 | if(!ip) |
gunarthon | 42:b69538bba4f9 | 362 | while(1); //terminate program |
gunarthon | 42:b69538bba4f9 | 363 | |
gunarthon | 42:b69538bba4f9 | 364 | Thread::wait(5000); |
gunarthon | 42:b69538bba4f9 | 365 | udpThread.start(UdpMethode); |
gunarthon | 42:b69538bba4f9 | 366 | } |
gunarthon | 42:b69538bba4f9 | 367 | else |
gunarthon | 42:b69538bba4f9 | 368 | serialThread.start(SerialMethode); |
gunarthon | 42:b69538bba4f9 | 369 | |
gunarthon | 38:b760c09b311c | 370 | pidController = new Pid(Kp, Ki, Kd); |
gunarthon | 38:b760c09b311c | 371 | pidController->setSetPoint(setPoint); |
gunarthon | 38:b760c09b311c | 372 | |
gunarthon | 41:3bc2a3885b9d | 373 | lcdThread.start(LcdMethode); |
gunarthon | 38:b760c09b311c | 374 | pidThread.start(PidMethode); |
gunarthon | 38:b760c09b311c | 375 | hmiThread.start(hmiMethode); |
gunarthon | 41:3bc2a3885b9d | 376 | setPointThread.start(setPointMethode); |
gunarthon | 38:b760c09b311c | 377 | |
gunarthon | 38:b760c09b311c | 378 | while (true) { |
gunarthon | 43:5123f24e0b2c | 379 | Thread::wait(1000); |
gunarthon | 43:5123f24e0b2c | 380 | led3 != led3; |
gunarthon | 38:b760c09b311c | 381 | } |
gunarthon | 38:b760c09b311c | 382 | } |
gunarthon | 38:b760c09b311c | 383 | |
gunarthon | 38:b760c09b311c | 384 | //============================================================================== |
gunarthon | 38:b760c09b311c | 385 | |
gunarthon | 38:b760c09b311c | 386 | |
gunarthon | 38:b760c09b311c | 387 |