fsdfds

Dependencies:   mbed

Committer:
teamat
Date:
Wed May 20 15:07:31 2020 +0000
Revision:
8:b3ce040fdebc
Parent:
7:ca62dda005d5
Child:
9:e8c6a414e226
Its alive

Who changed what in which revision?

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