Successful acro and level mode now! Relying on MPU9250 as base sensor. I'm working continuously on tuning and features :) NEWEST VERSION ON: https://github.com/MaEtUgR/FlyBed (CODE 100% compatible/copyable)
RC/RC_Channel.cpp@8:609a2ad4c30e, 2015-11-19 (annotated)
- Committer:
- maetugr
- Date:
- Thu Nov 19 18:47:27 2015 +0000
- Revision:
- 8:609a2ad4c30e
- Parent:
- 1:60882db03b0f
made I2C-Sensors working in parallel, added rolling buffer for PID derivative, played with the PID and frequency parameters in main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maetugr | 0:37f0c1e8fa66 | 1 | #include "RC_Channel.h" |
maetugr | 0:37f0c1e8fa66 | 2 | #include "mbed.h" |
maetugr | 0:37f0c1e8fa66 | 3 | |
maetugr | 0:37f0c1e8fa66 | 4 | RC_Channel::RC_Channel(PinName mypin, int index) : myinterrupt(mypin) |
maetugr | 0:37f0c1e8fa66 | 5 | { |
maetugr | 0:37f0c1e8fa66 | 6 | LocalFileSystem local("local"); // If theres no local yet |
maetugr | 0:37f0c1e8fa66 | 7 | RC_Channel::index = index; |
maetugr | 0:37f0c1e8fa66 | 8 | time = -100; // start value to see if there was any value yet |
maetugr | 0:37f0c1e8fa66 | 9 | |
maetugr | 0:37f0c1e8fa66 | 10 | loadCalibrationValue(&scale, "SCALE"); |
maetugr | 0:37f0c1e8fa66 | 11 | loadCalibrationValue(&offset, "OFFSE"); |
maetugr | 0:37f0c1e8fa66 | 12 | |
maetugr | 0:37f0c1e8fa66 | 13 | myinterrupt.rise(this, &RC_Channel::rise); |
maetugr | 0:37f0c1e8fa66 | 14 | myinterrupt.fall(this, &RC_Channel::fall); |
maetugr | 0:37f0c1e8fa66 | 15 | timeoutchecker.attach(this, &RC_Channel::timeoutcheck, 1); |
maetugr | 0:37f0c1e8fa66 | 16 | } |
maetugr | 0:37f0c1e8fa66 | 17 | |
maetugr | 0:37f0c1e8fa66 | 18 | int RC_Channel::read() |
maetugr | 0:37f0c1e8fa66 | 19 | { |
maetugr | 0:37f0c1e8fa66 | 20 | if(time == -100) |
maetugr | 0:37f0c1e8fa66 | 21 | return time; |
maetugr | 0:37f0c1e8fa66 | 22 | float result = scale * (float)(time) + offset; // calibration of the readings |
maetugr | 0:37f0c1e8fa66 | 23 | return result; |
maetugr | 0:37f0c1e8fa66 | 24 | //return (result > 490 && result < 510) ? 500 : result; // make the middle of the stickpositions non drifting |
maetugr | 0:37f0c1e8fa66 | 25 | } |
maetugr | 0:37f0c1e8fa66 | 26 | |
maetugr | 0:37f0c1e8fa66 | 27 | void RC_Channel::rise() |
maetugr | 0:37f0c1e8fa66 | 28 | { |
maetugr | 0:37f0c1e8fa66 | 29 | timer.start(); |
maetugr | 0:37f0c1e8fa66 | 30 | } |
maetugr | 0:37f0c1e8fa66 | 31 | |
maetugr | 0:37f0c1e8fa66 | 32 | void RC_Channel::fall() |
maetugr | 0:37f0c1e8fa66 | 33 | { |
maetugr | 0:37f0c1e8fa66 | 34 | timer.stop(); |
maetugr | 0:37f0c1e8fa66 | 35 | int tester = timer.read_us(); |
maetugr | 0:37f0c1e8fa66 | 36 | if(tester >= 1000 && tester <=2000) |
maetugr | 0:37f0c1e8fa66 | 37 | time = tester-1000; // we want only the signal from 1000 - 2000 as 0 - 1000 for easier scaling |
maetugr | 0:37f0c1e8fa66 | 38 | timer.reset(); |
maetugr | 0:37f0c1e8fa66 | 39 | timer.start(); |
maetugr | 0:37f0c1e8fa66 | 40 | } |
maetugr | 0:37f0c1e8fa66 | 41 | |
maetugr | 0:37f0c1e8fa66 | 42 | void RC_Channel::timeoutcheck() |
maetugr | 0:37f0c1e8fa66 | 43 | { |
maetugr | 0:37f0c1e8fa66 | 44 | if (timer.read() > 0.3) |
maetugr | 0:37f0c1e8fa66 | 45 | time = -100; |
maetugr | 0:37f0c1e8fa66 | 46 | } |
maetugr | 0:37f0c1e8fa66 | 47 | |
maetugr | 0:37f0c1e8fa66 | 48 | void RC_Channel::saveCalibrationValue(float * value, char * fileextension) |
maetugr | 0:37f0c1e8fa66 | 49 | { |
maetugr | 0:37f0c1e8fa66 | 50 | char path[40]; |
maetugr | 0:37f0c1e8fa66 | 51 | sprintf(path, "/local/FlyBed/RC_%d_%s", index, fileextension); |
maetugr | 0:37f0c1e8fa66 | 52 | FILE *fp = fopen(path, "w"); |
maetugr | 0:37f0c1e8fa66 | 53 | if (fp != NULL) { |
maetugr | 1:60882db03b0f | 54 | fprintf(fp, "%f", *value); |
maetugr | 0:37f0c1e8fa66 | 55 | fclose(fp); |
maetugr | 0:37f0c1e8fa66 | 56 | } else |
maetugr | 0:37f0c1e8fa66 | 57 | value = 0; |
maetugr | 0:37f0c1e8fa66 | 58 | } |
maetugr | 0:37f0c1e8fa66 | 59 | |
maetugr | 0:37f0c1e8fa66 | 60 | void RC_Channel::loadCalibrationValue(float * value, char * fileextension) |
maetugr | 0:37f0c1e8fa66 | 61 | { |
maetugr | 0:37f0c1e8fa66 | 62 | char path[40]; |
maetugr | 0:37f0c1e8fa66 | 63 | sprintf(path, "/local/RC%d%s", index, fileextension); |
maetugr | 0:37f0c1e8fa66 | 64 | FILE *fp = fopen(path, "r"); |
maetugr | 0:37f0c1e8fa66 | 65 | if (fp != NULL) { |
maetugr | 0:37f0c1e8fa66 | 66 | fscanf(fp, "%f", value); |
maetugr | 0:37f0c1e8fa66 | 67 | fclose(fp); |
maetugr | 0:37f0c1e8fa66 | 68 | } else |
maetugr | 0:37f0c1e8fa66 | 69 | value = 0; |
maetugr | 0:37f0c1e8fa66 | 70 | } |