USNA ES456 Autonomous Vehicles / Mbed 2 deprecated MadPulse_Controller_ros

Dependencies:   mbed ServoOut BNO055_fusion ros_lib_kinetic MadPulseIMU ServoIn

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 > 490 && result < 510) ? 500 : result; // make the middle of the stickpositions non drifting
00024 }
00025 
00026 void RC_Channel::rise()
00027 {
00028     timer.start();
00029 }
00030 
00031 void RC_Channel::fall()
00032 {
00033     timer.stop();
00034     int tester = timer.read_us();
00035     if(tester >= 1000 && tester <=2000)
00036         time = tester-1000;  // we want only the signal from 1000 - 2000 as 0 - 1000 for easier scaling
00037     timer.reset();
00038     timer.start();
00039 }
00040 
00041 void RC_Channel::timeoutcheck()
00042 {
00043     if (timer.read() > 0.3)
00044         time = -100;
00045 }
00046 
00047 void RC_Channel::saveCalibrationValue(float * value, char * fileextension)
00048 {
00049     char path[40];
00050     sprintf(path, "/local/FlyBed/RC_%d_%s", index, fileextension);
00051     FILE *fp = fopen(path, "w");
00052     if (fp != NULL) {
00053         fprintf(fp, "%f", value);
00054         fclose(fp);
00055     } else
00056         value = 0;
00057 }
00058 
00059 void RC_Channel::loadCalibrationValue(float * value, char * fileextension)
00060 {
00061     char path[40];
00062     sprintf(path, "/local/RC%d%s", index, fileextension);
00063     FILE *fp = fopen(path, "r");
00064     if (fp != NULL) {
00065         fscanf(fp, "%f", value);
00066         fclose(fp);
00067     } else
00068         value = 0;
00069 }