H2M Teststand / Mbed 2 deprecated Lagerpruefstand

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Lagerpruefstand.cpp Source File

Lagerpruefstand.cpp

00001 #include "mbed.h"
00002 #include "Ticker.h"
00003 
00004 #define marker 16.0
00005 
00006 //***************************************************************************************************
00007 //Outputs
00008 
00009 DigitalOut led1(LED1);
00010 
00011 DigitalOut led2(LED2);
00012 
00013 DigitalOut led4(LED4);
00014 
00015 DigitalOut Motor(p14);
00016 
00017 PwmOut servo(p21);
00018 
00019 //***************************************************************************************************
00020 //Inputs
00021 
00022 InterruptIn input(p17);
00023 
00024 
00025 
00026 //***************************************************************************************************
00027 //Communication
00028 
00029 Serial pc(USBTX, USBRX); // tx, rx
00030 
00031 //***************************************************************************************************
00032 //Variables
00033 
00034 float sample;
00035 
00036 volatile float rpm = 0, rpm_total;
00037 
00038 bool newDetection = true;   //need this as multiple reads happen across white spot
00039 
00040 bool run_down = false;
00041 
00042 bool broadcast = false;
00043 
00044 bool broadcast_old = false;
00045 
00046 int samples[10];           //higher number = greater accuracy but longer read time
00047 
00048 int sampleCounts = 0, sampleCounts_old = 0;
00049 
00050 bool Motor_status = false;
00051 
00052 
00053 
00054 volatile int delta_t;
00055 
00056 
00057 //***************************************************************************************************
00058 //Misc
00059 
00060 Timer t;
00061 
00062 Ticker serial_tx;
00063 
00064 int seconds = 0;
00065 
00066 void rpm_counter()
00067 {
00068 
00069     sampleCounts++;
00070 
00071     led2 = !led2;
00072 
00073 }
00074 
00075 void servo_engage_movement()
00076 {
00077 
00078     servo.pulsewidth_us(1100 ); // servo position determined by a pulsewidth between 1-2ms
00079 
00080     //pc.printf("offset = %i us \n\r",offset);
00081 }
00082 
00083 void servo_disengage_movement()
00084 {
00085 
00086     servo.pulsewidth_us(1900); // servo position determined by a pulsewidth between 1-2ms
00087 
00088     // pc.printf("offset = %i us \n\r",offset);
00089 
00090 }
00091 
00092 
00093 void rpm_display()
00094 {
00095 
00096     rpm_total = sampleCounts/marker;
00097 
00098     rpm = ((rpm*4) + ((sampleCounts - sampleCounts_old)/ marker * 60))/5;
00099 
00100     sampleCounts_old = sampleCounts;
00101 
00102     if(run_down == false && broadcast == false && Motor_status == false) {
00103 
00104         servo_engage_movement();
00105 
00106         wait_ms(500);
00107 
00108         Motor = 0;
00109 
00110         Motor_status = true;
00111 
00112         sampleCounts = 0;
00113 
00114         sampleCounts_old = 0;
00115 
00116     }
00117 
00118     if(rpm > 505 && run_down == false) {
00119 
00120         servo_disengage_movement();
00121 
00122         Motor = 1;
00123 
00124         Motor_status = false;
00125 
00126         run_down = true;
00127     }
00128 
00129     if(run_down == true && rpm < 5) {
00130 
00131         run_down = false;
00132 
00133         broadcast = false;
00134     }
00135 
00136     if(run_down == true && rpm <= 500) {
00137 
00138         broadcast_old = broadcast;
00139 
00140         broadcast = true;
00141     }
00142 
00143 if(broadcast != broadcast_old) {
00144 
00145         seconds = 0;
00146 
00147         //pc.printf("Beginn der Drehzahlaufzeichnung\n\r");
00148     }
00149 
00150     if(broadcast == true) {
00151 
00152         pc.printf("$1;1;%i;%f;0\r\n",seconds,rpm);
00153         
00154         ++seconds;
00155         
00156     }
00157         
00158     led1 = !led1;
00159 
00160 }
00161 
00162 
00163 int main(void)
00164 {
00165 
00166     Motor = 1;
00167 
00168     Motor_status = false;
00169 
00170     servo.period(0.020);          // servo requires a 20ms period
00171 
00172     t.start(); //start initial timer
00173 
00174     servo.pulsewidth_us(2000);
00175 
00176     input.rise(&rpm_counter);
00177 
00178     serial_tx.attach(&rpm_display,1); // the function rpm_display attached to the ticker wich is using the interval of 1 second
00179 
00180 
00181 
00182     while(true) {
00183 
00184 
00185 
00186         led4 = !run_down;
00187 
00188     }
00189 }