Artem Solomatin
/
pendudu
fsdfds
main.cpp@12:ee175985ef09, 2020-05-23 (annotated)
- Committer:
- teamat
- Date:
- Sat May 23 15:24:09 2020 +0000
- Revision:
- 12:ee175985ef09
- Parent:
- 11:6bbe8da4b16a
- Child:
- 13:205002e3c176
pobeda nad getcharom
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
teamat | 3:8708e61475fe | 1 | #include "mbed.h" |
teamat | 3:8708e61475fe | 2 | |
teamat | 3:8708e61475fe | 3 | #define M_PI 3.14159265358979323846f |
teamat | 3:8708e61475fe | 4 | #define smoothingNumber 6 |
teamat | 3:8708e61475fe | 5 | |
teamat | 12:ee175985ef09 | 6 | |
teamat | 3:8708e61475fe | 7 | #define STATE_ERROR 0 |
teamat | 3:8708e61475fe | 8 | #define STATE_INIT 1 |
teamat | 3:8708e61475fe | 9 | #define STATE_GOTO_START 2 |
teamat | 3:8708e61475fe | 10 | #define STATE_GOTO_END_COUNTING 3 |
teamat | 3:8708e61475fe | 11 | #define STATE_GOTO_MIDDLE 4 |
teamat | 3:8708e61475fe | 12 | #define STATE_WAITING 5 |
teamat | 3:8708e61475fe | 13 | #define STATE_GOTO_SWING 6 |
teamat | 3:8708e61475fe | 14 | #define STATE_START_SWING 7 |
teamat | 3:8708e61475fe | 15 | #define STATE_SWING_RIGHT 8 |
teamat | 3:8708e61475fe | 16 | #define STATE_SWING_LEFT 9 |
teamat | 3:8708e61475fe | 17 | #define STATE_EMPTY_SWING 10 |
teamat | 3:8708e61475fe | 18 | |
teamat | 3:8708e61475fe | 19 | #define DIR_LEFT 0 |
teamat | 3:8708e61475fe | 20 | #define DIR_RIGHT 1 |
teamat | 3:8708e61475fe | 21 | |
teamat | 12:ee175985ef09 | 22 | #define timer_period_us 10 |
teamat | 12:ee175985ef09 | 23 | |
teamat | 12:ee175985ef09 | 24 | |
teamat | 3:8708e61475fe | 25 | DigitalOut RCout(D10); |
teamat | 3:8708e61475fe | 26 | DigitalOut dir(D9); |
teamat | 3:8708e61475fe | 27 | InterruptIn leftSwitch(D2); |
teamat | 3:8708e61475fe | 28 | InterruptIn rightSwitch(D3); |
teamat | 3:8708e61475fe | 29 | Ticker tick; |
teamat | 12:ee175985ef09 | 30 | //Ticker speedTicker; |
teamat | 4:fd49edfabfb2 | 31 | Ticker swingTicker; |
teamat | 3:8708e61475fe | 32 | RawSerial rpc(D1,D0,9600); |
teamat | 12:ee175985ef09 | 33 | DigitalOut led(D4); |
teamat | 11:6bbe8da4b16a | 34 | |
teamat | 12:ee175985ef09 | 35 | int period_us = 1;//300 26 |
teamat | 7:ca62dda005d5 | 36 | int posCounter = 0; |
teamat | 12:ee175985ef09 | 37 | int timerCounter = 0; |
teamat | 3:8708e61475fe | 38 | |
teamat | 3:8708e61475fe | 39 | long pos=0; |
teamat | 3:8708e61475fe | 40 | |
teamat | 3:8708e61475fe | 41 | long railLength = 0; |
teamat | 3:8708e61475fe | 42 | |
teamat | 3:8708e61475fe | 43 | uint8_t state=STATE_INIT; |
teamat | 12:ee175985ef09 | 44 | uint8_t data[2] = {0, 0}; |
teamat | 12:ee175985ef09 | 45 | uint8_t cmdIndex = 0; |
teamat | 3:8708e61475fe | 46 | |
teamat | 3:8708e61475fe | 47 | Timer t; |
teamat | 3:8708e61475fe | 48 | SPI spi(D11,D12,D13);// mosi, miso, sclk |
teamat | 3:8708e61475fe | 49 | DigitalOut cs(D5); |
teamat | 3:8708e61475fe | 50 | |
teamat | 4:fd49edfabfb2 | 51 | float radius = 0.0025; |
teamat | 3:8708e61475fe | 52 | |
teamat | 3:8708e61475fe | 53 | float angularPosNew = 0; |
teamat | 3:8708e61475fe | 54 | float angularPosOld = 0; |
teamat | 3:8708e61475fe | 55 | |
teamat | 3:8708e61475fe | 56 | float timeOld = 0.0f; |
teamat | 3:8708e61475fe | 57 | float timeStart = 0.0f; |
teamat | 3:8708e61475fe | 58 | |
teamat | 3:8708e61475fe | 59 | float dx = 0; |
teamat | 3:8708e61475fe | 60 | float xPosNew = 0; |
teamat | 3:8708e61475fe | 61 | float xPosOld = 0; |
teamat | 8:b3ce040fdebc | 62 | float speed = -1; |
teamat | 8:b3ce040fdebc | 63 | float posOffset = 0.0f; |
teamat | 7:ca62dda005d5 | 64 | float posMap[4] = {0.0f, 0.0f, 0.0f, 0.0f}; |
teamat | 3:8708e61475fe | 65 | |
teamat | 3:8708e61475fe | 66 | double dAngle = 0.0f; |
teamat | 3:8708e61475fe | 67 | double anSpd = 0.0f; |
teamat | 3:8708e61475fe | 68 | |
teamat | 3:8708e61475fe | 69 | float timeOldPos = 0.0f; |
teamat | 3:8708e61475fe | 70 | float timeStartPos = 0.0f; |
teamat | 3:8708e61475fe | 71 | |
teamat | 4:fd49edfabfb2 | 72 | float angle=0.0f; |
teamat | 3:8708e61475fe | 73 | float angleOffset = 0; |
teamat | 3:8708e61475fe | 74 | |
teamat | 4:fd49edfabfb2 | 75 | float control = 0.0f; |
teamat | 4:fd49edfabfb2 | 76 | |
teamat | 3:8708e61475fe | 77 | double PIPI = 6.28; |
teamat | 3:8708e61475fe | 78 | |
teamat | 3:8708e61475fe | 79 | bool canSend = false; |
teamat | 8:b3ce040fdebc | 80 | bool periodUpdated = false; |
teamat | 8:b3ce040fdebc | 81 | bool calibrated = false; |
teamat | 9:e8c6a414e226 | 82 | bool dirUpdated = false; |
teamat | 3:8708e61475fe | 83 | |
teamat | 3:8708e61475fe | 84 | typedef union { |
teamat | 4:fd49edfabfb2 | 85 | float number[6]; |
teamat | 4:fd49edfabfb2 | 86 | uint8_t numberCh[24]; |
teamat | 3:8708e61475fe | 87 | } my_union; |
teamat | 3:8708e61475fe | 88 | |
teamat | 3:8708e61475fe | 89 | my_union myUnion; |
teamat | 3:8708e61475fe | 90 | |
teamat | 12:ee175985ef09 | 91 | /*bool isPendulumSwinging() { |
teamat | 3:8708e61475fe | 92 | return state == STATE_SWING_RIGHT || state == STATE_SWING_LEFT; |
teamat | 12:ee175985ef09 | 93 | }*/ |
teamat | 3:8708e61475fe | 94 | |
teamat | 3:8708e61475fe | 95 | float getPosMM() { |
teamat | 3:8708e61475fe | 96 | //return (pos-railLength/2) * 550.0f/railLength; |
teamat | 3:8708e61475fe | 97 | //return (pos - railLength / 2) * (350.0f / railLength); |
teamat | 3:8708e61475fe | 98 | return pos / 3500.0f; |
teamat | 3:8708e61475fe | 99 | } |
teamat | 3:8708e61475fe | 100 | |
teamat | 3:8708e61475fe | 101 | uint16_t getPendulumPos(){ |
teamat | 3:8708e61475fe | 102 | cs=0; |
teamat | 3:8708e61475fe | 103 | wait_ms(1); |
teamat | 3:8708e61475fe | 104 | uint16_t d=spi.write((short)0x00); |
teamat | 3:8708e61475fe | 105 | d=d<<1;//fucking shithole fakebit |
teamat | 3:8708e61475fe | 106 | d=d>>6;//no need debug info |
teamat | 3:8708e61475fe | 107 | cs=1; |
teamat | 3:8708e61475fe | 108 | wait_ms(1); |
teamat | 3:8708e61475fe | 109 | return (uint16_t)d; |
teamat | 3:8708e61475fe | 110 | } |
teamat | 3:8708e61475fe | 111 | |
teamat | 3:8708e61475fe | 112 | |
teamat | 3:8708e61475fe | 113 | float getPendulumAngle(){ |
teamat | 3:8708e61475fe | 114 | angle = getPendulumPos(); |
teamat | 3:8708e61475fe | 115 | angle = angle * 6.28f / 1024.0f; |
teamat | 4:fd49edfabfb2 | 116 | angle += angleOffset; |
teamat | 8:b3ce040fdebc | 117 | if (angle > 3.14) { |
teamat | 8:b3ce040fdebc | 118 | angle = angle - PIPI; |
teamat | 8:b3ce040fdebc | 119 | } |
teamat | 8:b3ce040fdebc | 120 | /*if (angle > PIPI + 0.01) { |
teamat | 4:fd49edfabfb2 | 121 | angle = fmod((angle + 3.14), PIPI) - 3.14; |
teamat | 4:fd49edfabfb2 | 122 | if (angle < -3.14) { |
teamat | 4:fd49edfabfb2 | 123 | angle += PIPI; |
teamat | 4:fd49edfabfb2 | 124 | } |
teamat | 9:e8c6a414e226 | 125 | }*/ |
teamat | 8:b3ce040fdebc | 126 | /*if (calibrated) { |
teamat | 8:b3ce040fdebc | 127 | float test = fmodf(angle, M_PI); |
teamat | 8:b3ce040fdebc | 128 | if (test >= 1) { |
teamat | 8:b3ce040fdebc | 129 | angle = -test; |
teamat | 8:b3ce040fdebc | 130 | } else { |
teamat | 8:b3ce040fdebc | 131 | angle = test; |
teamat | 8:b3ce040fdebc | 132 | } |
teamat | 8:b3ce040fdebc | 133 | }*/ |
teamat | 3:8708e61475fe | 134 | return angle; |
teamat | 3:8708e61475fe | 135 | } |
teamat | 3:8708e61475fe | 136 | |
teamat | 3:8708e61475fe | 137 | |
teamat | 3:8708e61475fe | 138 | /* |
teamat | 3:8708e61475fe | 139 | ANGULAR SPEED CALC |
teamat | 3:8708e61475fe | 140 | */ |
teamat | 3:8708e61475fe | 141 | |
teamat | 3:8708e61475fe | 142 | void getDeltaAng(){ |
teamat | 3:8708e61475fe | 143 | |
teamat | 3:8708e61475fe | 144 | angularPosNew = getPendulumAngle(); |
teamat | 3:8708e61475fe | 145 | |
teamat | 3:8708e61475fe | 146 | dAngle = fmod((angularPosNew - angularPosOld + 3.14), PIPI) - 3.14; |
teamat | 3:8708e61475fe | 147 | if (dAngle < -3.14) { |
teamat | 3:8708e61475fe | 148 | dAngle += PIPI; |
teamat | 3:8708e61475fe | 149 | } |
teamat | 3:8708e61475fe | 150 | |
teamat | 3:8708e61475fe | 151 | angularPosOld = angularPosNew; |
teamat | 3:8708e61475fe | 152 | |
teamat | 3:8708e61475fe | 153 | } |
teamat | 3:8708e61475fe | 154 | |
teamat | 3:8708e61475fe | 155 | void getAngularSpeed(){ |
teamat | 3:8708e61475fe | 156 | float deltaTime; |
teamat | 3:8708e61475fe | 157 | |
teamat | 3:8708e61475fe | 158 | timeStart = float(t.read()); |
teamat | 3:8708e61475fe | 159 | deltaTime = (timeStart - timeOld); |
teamat | 3:8708e61475fe | 160 | getDeltaAng(); |
teamat | 3:8708e61475fe | 161 | anSpd = dAngle / deltaTime; |
teamat | 3:8708e61475fe | 162 | timeOld=timeStart; |
teamat | 3:8708e61475fe | 163 | //взятие по модулю, спросить |
teamat | 3:8708e61475fe | 164 | } |
teamat | 3:8708e61475fe | 165 | |
teamat | 3:8708e61475fe | 166 | /* |
teamat | 3:8708e61475fe | 167 | SPEED CALC |
teamat | 3:8708e61475fe | 168 | */ |
teamat | 3:8708e61475fe | 169 | |
teamat | 12:ee175985ef09 | 170 | /*void calcSpeed() { |
teamat | 7:ca62dda005d5 | 171 | /*posMap[posCounter] = getPosMM() / 1000; |
teamat | 7:ca62dda005d5 | 172 | if (posCounter == 3) { |
teamat | 7:ca62dda005d5 | 173 | posCounter = 0; |
teamat | 7:ca62dda005d5 | 174 | float pathSum = 0; |
teamat | 7:ca62dda005d5 | 175 | for (int i = 0; i < 3; i++) { |
teamat | 7:ca62dda005d5 | 176 | float pathDiff = posMap[i + 1] - posMap[i]; |
teamat | 7:ca62dda005d5 | 177 | if ((pathDiff) < 0) { |
teamat | 7:ca62dda005d5 | 178 | pathSum += -pathDiff; |
teamat | 7:ca62dda005d5 | 179 | } else { |
teamat | 7:ca62dda005d5 | 180 | pathSum += pathDiff; |
teamat | 7:ca62dda005d5 | 181 | } |
teamat | 7:ca62dda005d5 | 182 | } |
teamat | 7:ca62dda005d5 | 183 | speed = pathSum; |
teamat | 7:ca62dda005d5 | 184 | } else { |
teamat | 7:ca62dda005d5 | 185 | posCounter += 1; |
teamat | 7:ca62dda005d5 | 186 | }*/ |
teamat | 4:fd49edfabfb2 | 187 | |
teamat | 12:ee175985ef09 | 188 | /*void calcControl() { |
teamat | 6:999e8ae7d969 | 189 | //float frequency = 1000000 / (period_us / 2); |
teamat | 6:999e8ae7d969 | 190 | //float rates = frequency / 6400; |
teamat | 6:999e8ae7d969 | 191 | //control = rates * PIPI * radius; |
teamat | 8:b3ce040fdebc | 192 | if (dir = DIR_RIGHT) { |
teamat | 8:b3ce040fdebc | 193 | control = -period_us; |
teamat | 8:b3ce040fdebc | 194 | } else { |
teamat | 8:b3ce040fdebc | 195 | control = period_us; |
teamat | 8:b3ce040fdebc | 196 | } |
teamat | 12:ee175985ef09 | 197 | }*/ |
teamat | 3:8708e61475fe | 198 | |
teamat | 3:8708e61475fe | 199 | |
teamat | 3:8708e61475fe | 200 | |
teamat | 3:8708e61475fe | 201 | void stepperFlip() { |
teamat | 12:ee175985ef09 | 202 | if (timerCounter * timer_period_us > period_us) { |
teamat | 12:ee175985ef09 | 203 | timerCounter = 0; |
teamat | 12:ee175985ef09 | 204 | if (state != STATE_WAITING && state != STATE_ERROR && state != STATE_INIT && state){ |
teamat | 12:ee175985ef09 | 205 | RCout = !RCout; |
teamat | 12:ee175985ef09 | 206 | pos += (dir.read() * 2 - 1); |
teamat | 12:ee175985ef09 | 207 | } |
teamat | 12:ee175985ef09 | 208 | if (state == STATE_GOTO_MIDDLE && pos == railLength / 2) { |
teamat | 12:ee175985ef09 | 209 | posOffset = 40; |
teamat | 12:ee175985ef09 | 210 | state = STATE_WAITING; |
teamat | 12:ee175985ef09 | 211 | } |
teamat | 12:ee175985ef09 | 212 | if (state == STATE_SWING_LEFT && state==STATE_SWING_RIGHT) { |
teamat | 12:ee175985ef09 | 213 | pos += (dir.read() * 2 - 1); |
teamat | 12:ee175985ef09 | 214 | RCout = !RCout; |
teamat | 12:ee175985ef09 | 215 | } |
teamat | 12:ee175985ef09 | 216 | } else { |
teamat | 12:ee175985ef09 | 217 | timerCounter += 1; |
teamat | 3:8708e61475fe | 218 | } |
teamat | 3:8708e61475fe | 219 | } |
teamat | 3:8708e61475fe | 220 | |
teamat | 3:8708e61475fe | 221 | |
teamat | 12:ee175985ef09 | 222 | /*void updatePeriod(){ |
teamat | 12:ee175985ef09 | 223 | tick.detach(); |
teamat | 3:8708e61475fe | 224 | tick.attach_us (&stepperFlip, period_us / 2.0f); |
teamat | 12:ee175985ef09 | 225 | wait_ms(20); |
teamat | 12:ee175985ef09 | 226 | }*/ |
teamat | 3:8708e61475fe | 227 | |
teamat | 3:8708e61475fe | 228 | void leftEnd() { |
teamat | 3:8708e61475fe | 229 | dir = DIR_RIGHT; |
teamat | 3:8708e61475fe | 230 | if (state == STATE_GOTO_START) { |
teamat | 3:8708e61475fe | 231 | state = STATE_GOTO_END_COUNTING; |
teamat | 3:8708e61475fe | 232 | pos = 0; |
teamat | 3:8708e61475fe | 233 | } |
teamat | 12:ee175985ef09 | 234 | /* else if (isPendulumSwinging()) { |
teamat | 9:e8c6a414e226 | 235 | //state = STATE_GOTO_MIDDLE; |
teamat | 3:8708e61475fe | 236 | //angleOffset -= 0.006191; |
teamat | 9:e8c6a414e226 | 237 | state = STATE_ERROR; |
teamat | 12:ee175985ef09 | 238 | }*/ |
teamat | 9:e8c6a414e226 | 239 | if (state == STATE_GOTO_SWING) { |
teamat | 9:e8c6a414e226 | 240 | state = STATE_ERROR; |
teamat | 9:e8c6a414e226 | 241 | } |
teamat | 3:8708e61475fe | 242 | } |
teamat | 3:8708e61475fe | 243 | |
teamat | 3:8708e61475fe | 244 | void rightEnd() { |
teamat | 3:8708e61475fe | 245 | dir=DIR_LEFT; |
teamat | 3:8708e61475fe | 246 | if (state == STATE_GOTO_END_COUNTING) { |
teamat | 3:8708e61475fe | 247 | railLength=pos; |
teamat | 3:8708e61475fe | 248 | state = STATE_GOTO_MIDDLE; |
teamat | 3:8708e61475fe | 249 | } |
teamat | 12:ee175985ef09 | 250 | /* else if (isPendulumSwinging()) { |
teamat | 9:e8c6a414e226 | 251 | //state = STATE_GOTO_MIDDLE; |
teamat | 3:8708e61475fe | 252 | //angleOffset += 0.006191; |
teamat | 9:e8c6a414e226 | 253 | state = STATE_ERROR; |
teamat | 12:ee175985ef09 | 254 | }*/ |
teamat | 9:e8c6a414e226 | 255 | if (state == STATE_GOTO_SWING) { |
teamat | 9:e8c6a414e226 | 256 | state = STATE_ERROR; |
teamat | 3:8708e61475fe | 257 | } |
teamat | 3:8708e61475fe | 258 | } |
teamat | 3:8708e61475fe | 259 | |
teamat | 3:8708e61475fe | 260 | |
teamat | 3:8708e61475fe | 261 | void sendData() { |
teamat | 3:8708e61475fe | 262 | myUnion.number[0] = t.read(); |
teamat | 8:b3ce040fdebc | 263 | myUnion.number[1] = (getPosMM()- posOffset) / 1000 ; |
teamat | 4:fd49edfabfb2 | 264 | myUnion.number[2] = speed; |
teamat | 4:fd49edfabfb2 | 265 | //myUnion.number[3] = dAngle; |
teamat | 4:fd49edfabfb2 | 266 | myUnion.number[3] = angle; |
teamat | 3:8708e61475fe | 267 | myUnion.number[4] = anSpd; |
teamat | 4:fd49edfabfb2 | 268 | myUnion.number[5] = control; |
teamat | 4:fd49edfabfb2 | 269 | for(int i = 0; i < 24; i++) { |
teamat | 3:8708e61475fe | 270 | rpc.putc(myUnion.numberCh[i]); |
teamat | 3:8708e61475fe | 271 | } |
teamat | 3:8708e61475fe | 272 | } |
teamat | 3:8708e61475fe | 273 | |
teamat | 10:ca38e4d775c8 | 274 | |
teamat | 12:ee175985ef09 | 275 | void changeDir() { |
teamat | 12:ee175985ef09 | 276 | if (dir == DIR_LEFT) { |
teamat | 12:ee175985ef09 | 277 | dir = DIR_RIGHT; |
teamat | 12:ee175985ef09 | 278 | } else { |
teamat | 12:ee175985ef09 | 279 | dir = DIR_LEFT; |
teamat | 12:ee175985ef09 | 280 | } |
teamat | 12:ee175985ef09 | 281 | } |
teamat | 12:ee175985ef09 | 282 | |
teamat | 12:ee175985ef09 | 283 | int command[2] = {0, 0}; |
teamat | 12:ee175985ef09 | 284 | bool newData = false; |
teamat | 10:ca38e4d775c8 | 285 | |
teamat | 3:8708e61475fe | 286 | void Rx_interrupt() { |
teamat | 12:ee175985ef09 | 287 | data[cmdIndex] = rpc.getc(); |
teamat | 12:ee175985ef09 | 288 | if (cmdIndex == 1) { |
teamat | 12:ee175985ef09 | 289 | newData = true; |
teamat | 12:ee175985ef09 | 290 | } |
teamat | 12:ee175985ef09 | 291 | cmdIndex = !cmdIndex; |
teamat | 12:ee175985ef09 | 292 | /*command[0] = rpc.getc(); |
teamat | 12:ee175985ef09 | 293 | if (command[0] == 50) { |
teamat | 12:ee175985ef09 | 294 | canSend = true; |
teamat | 12:ee175985ef09 | 295 | }*/ |
teamat | 12:ee175985ef09 | 296 | /* command[1] = rpc.getc(); |
teamat | 12:ee175985ef09 | 297 | switch (command[0]) { |
teamat | 8:b3ce040fdebc | 298 | case 50: |
teamat | 8:b3ce040fdebc | 299 | canSend = true; |
teamat | 8:b3ce040fdebc | 300 | break; |
teamat | 8:b3ce040fdebc | 301 | case 60: |
teamat | 12:ee175985ef09 | 302 | swingTicker.attach(changeDir, 2); |
teamat | 8:b3ce040fdebc | 303 | state = STATE_GOTO_SWING; |
teamat | 8:b3ce040fdebc | 304 | break; |
teamat | 8:b3ce040fdebc | 305 | case 70: |
teamat | 12:ee175985ef09 | 306 | period_us = command[1]; |
teamat | 8:b3ce040fdebc | 307 | break; |
teamat | 8:b3ce040fdebc | 308 | default: |
teamat | 12:ee175985ef09 | 309 | break; |
teamat | 12:ee175985ef09 | 310 | } */ |
teamat | 3:8708e61475fe | 311 | } |
teamat | 3:8708e61475fe | 312 | |
teamat | 12:ee175985ef09 | 313 | void proceedCommands() { |
teamat | 12:ee175985ef09 | 314 | newData = false; |
teamat | 12:ee175985ef09 | 315 | switch (data[0]) { |
teamat | 12:ee175985ef09 | 316 | case 50: |
teamat | 12:ee175985ef09 | 317 | canSend = true; |
teamat | 12:ee175985ef09 | 318 | break; |
teamat | 12:ee175985ef09 | 319 | case 60: |
teamat | 12:ee175985ef09 | 320 | /*swingTicker.attach(changeDir, 2); |
teamat | 12:ee175985ef09 | 321 | state = STATE_GOTO_SWING;*/ |
teamat | 12:ee175985ef09 | 322 | break; |
teamat | 12:ee175985ef09 | 323 | case 70: |
teamat | 12:ee175985ef09 | 324 | period_us = data[1]; |
teamat | 12:ee175985ef09 | 325 | control = period_us; |
teamat | 12:ee175985ef09 | 326 | break; |
teamat | 12:ee175985ef09 | 327 | default: |
teamat | 12:ee175985ef09 | 328 | break; |
teamat | 12:ee175985ef09 | 329 | } |
teamat | 12:ee175985ef09 | 330 | } |
teamat | 12:ee175985ef09 | 331 | |
teamat | 12:ee175985ef09 | 332 | /*void govnina() { |
teamat | 12:ee175985ef09 | 333 | if (timerCounter * timer_period_us > period_us) { |
teamat | 12:ee175985ef09 | 334 | led = !led; |
teamat | 12:ee175985ef09 | 335 | timerCounter = 0; |
teamat | 12:ee175985ef09 | 336 | } else { |
teamat | 12:ee175985ef09 | 337 | timerCounter += 1; |
teamat | 12:ee175985ef09 | 338 | } |
teamat | 11:6bbe8da4b16a | 339 | }*/ |
teamat | 11:6bbe8da4b16a | 340 | |
teamat | 12:ee175985ef09 | 341 | |
teamat | 12:ee175985ef09 | 342 | |
teamat | 3:8708e61475fe | 343 | int main() { |
teamat | 3:8708e61475fe | 344 | RCout = 1; |
teamat | 3:8708e61475fe | 345 | wait_ms(500); |
teamat | 3:8708e61475fe | 346 | spi.format(16,2); |
teamat | 3:8708e61475fe | 347 | spi.frequency(1000000); |
teamat | 3:8708e61475fe | 348 | t.start(); |
teamat | 3:8708e61475fe | 349 | leftSwitch.rise(&leftEnd); |
teamat | 3:8708e61475fe | 350 | rightSwitch.rise(&rightEnd); |
teamat | 4:fd49edfabfb2 | 351 | rpc.attach(&Rx_interrupt, Serial::RxIrq); |
teamat | 3:8708e61475fe | 352 | for (int i=5; i>0; i--) { |
teamat | 3:8708e61475fe | 353 | getPendulumAngle(); |
teamat | 3:8708e61475fe | 354 | wait_ms(500); |
teamat | 3:8708e61475fe | 355 | } |
teamat | 4:fd49edfabfb2 | 356 | angleOffset= 3.14 - angle; |
teamat | 8:b3ce040fdebc | 357 | calibrated = true; |
teamat | 8:b3ce040fdebc | 358 | //wait(12); |
teamat | 12:ee175985ef09 | 359 | //wait(3); |
teamat | 11:6bbe8da4b16a | 360 | //updatePeriod(); |
teamat | 12:ee175985ef09 | 361 | //calcControl(); |
teamat | 12:ee175985ef09 | 362 | tick.attach_us(stepperFlip, timer_period_us); |
teamat | 3:8708e61475fe | 363 | state=STATE_GOTO_START; |
teamat | 3:8708e61475fe | 364 | dir=DIR_LEFT; |
teamat | 12:ee175985ef09 | 365 | swingTicker.attach(changeDir, 2); |
teamat | 3:8708e61475fe | 366 | while(1) { |
teamat | 4:fd49edfabfb2 | 367 | getAngularSpeed(); |
teamat | 8:b3ce040fdebc | 368 | if (canSend) { |
teamat | 3:8708e61475fe | 369 | sendData(); |
teamat | 3:8708e61475fe | 370 | } |
teamat | 12:ee175985ef09 | 371 | if (newData) { |
teamat | 12:ee175985ef09 | 372 | proceedCommands(); |
teamat | 12:ee175985ef09 | 373 | } |
teamat | 12:ee175985ef09 | 374 | /*if (periodUpdated) { |
teamat | 8:b3ce040fdebc | 375 | calcControl(); |
teamat | 12:ee175985ef09 | 376 | updatePeriod(); |
teamat | 8:b3ce040fdebc | 377 | periodUpdated = false; |
teamat | 12:ee175985ef09 | 378 | }*/ |
teamat | 3:8708e61475fe | 379 | switch(state) { |
teamat | 3:8708e61475fe | 380 | case STATE_WAITING: |
teamat | 8:b3ce040fdebc | 381 | //state = STATE_GOTO_SWING; |
teamat | 3:8708e61475fe | 382 | break; |
teamat | 3:8708e61475fe | 383 | case STATE_GOTO_START: |
teamat | 3:8708e61475fe | 384 | break; |
teamat | 3:8708e61475fe | 385 | case STATE_GOTO_END_COUNTING: |
teamat | 3:8708e61475fe | 386 | break; |
teamat | 3:8708e61475fe | 387 | case STATE_GOTO_SWING: |
teamat | 9:e8c6a414e226 | 388 | //speedTicker.attach(calcSpeed, 1); |
teamat | 8:b3ce040fdebc | 389 | //swingTicker.attach(getSwingDirectory, 1); |
teamat | 8:b3ce040fdebc | 390 | //state = STATE_SWING_LEFT; |
teamat | 3:8708e61475fe | 391 | break; |
teamat | 3:8708e61475fe | 392 | case STATE_SWING_LEFT: |
teamat | 9:e8c6a414e226 | 393 | /*if (swingCounter == 6) { |
teamat | 9:e8c6a414e226 | 394 | swingCounter = 0; |
teamat | 9:e8c6a414e226 | 395 | period_us += 4; |
teamat | 9:e8c6a414e226 | 396 | periodUpdated = true; |
teamat | 9:e8c6a414e226 | 397 | }*/ |
teamat | 3:8708e61475fe | 398 | break; |
teamat | 3:8708e61475fe | 399 | case STATE_SWING_RIGHT: |
teamat | 9:e8c6a414e226 | 400 | /*if (swingCounter == 6) { |
teamat | 9:e8c6a414e226 | 401 | swingCounter = 0; |
teamat | 9:e8c6a414e226 | 402 | period_us += 4; |
teamat | 9:e8c6a414e226 | 403 | periodUpdated = true; |
teamat | 9:e8c6a414e226 | 404 | }*/ |
teamat | 3:8708e61475fe | 405 | break; |
teamat | 3:8708e61475fe | 406 | default: |
teamat | 3:8708e61475fe | 407 | break; |
teamat | 3:8708e61475fe | 408 | } |
teamat | 3:8708e61475fe | 409 | } |
teamat | 3:8708e61475fe | 410 | } |