My fully self designed first stable working Quadrocopter Software.

Dependencies:   mbed

Dependents:   fluy343

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RC_Channel.cpp Source File

RC_Channel.cpp

00001 #include "RC_Channel.h"
00002 #include "mbed.h"
00003 
00004 RC_Channel::RC_Channel(PinName mypin, int index) : myinterrupt(mypin)
00005 {
00006     LocalFileSystem local("local");                                  // If theres no local yet
00007     RC_Channel::index = index;
00008     time = -100; // start value to see if there was any value yet
00009     
00010     loadCalibrationValue(&scale, "SCALE");
00011     loadCalibrationValue(&offset, "OFFSE");
00012     
00013     myinterrupt.rise(this, &RC_Channel::rise);
00014     myinterrupt.fall(this, &RC_Channel::fall);
00015     timeoutchecker.attach(this, &RC_Channel::timeoutcheck, 1);
00016 }
00017 
00018 int RC_Channel::read()
00019 {
00020     if(time == -100)
00021         return time;
00022     float result = scale * (float)(time) + offset; // calibration of the readings
00023     return result;
00024     //return (result > 490 && result < 510) ? 500 : result; // make the middle of the stickpositions non drifting
00025 }
00026 
00027 void RC_Channel::rise()
00028 {
00029     timer.start();
00030 }
00031 
00032 void RC_Channel::fall()
00033 {
00034     timer.stop();
00035     int tester = timer.read_us();
00036     if(tester >= 1000 && tester <=2000)
00037         time = tester-1000;  // we want only the signal from 1000 - 2000 as 0 - 1000 for easier scaling
00038     timer.reset();
00039     timer.start();
00040 }
00041 
00042 void RC_Channel::timeoutcheck()
00043 {
00044     if (timer.read() > 0.3)
00045         time = -100;
00046 }
00047 
00048 void RC_Channel::saveCalibrationValue(float * value, char * fileextension)
00049 {
00050     char path[40];
00051     sprintf(path, "/local/FlyBed/RC_%d_%s", index, fileextension);
00052     FILE *fp = fopen(path, "w");
00053     if (fp != NULL) {
00054         fprintf(fp, "%f", value);
00055         fclose(fp);
00056     } else
00057         value = 0;
00058 }
00059 
00060 void RC_Channel::loadCalibrationValue(float * value, char * fileextension)
00061 {
00062     char path[40];
00063     sprintf(path, "/local/RC%d%s", index, fileextension);
00064     FILE *fp = fopen(path, "r");
00065     if (fp != NULL) {
00066         fscanf(fp, "%f", value);
00067         fclose(fp);
00068     } else
00069         value = 0;
00070 }