Lagerpruefstand

Dependencies:   mbed

Lagerpruefstand.cpp

Committer:
O_Shovah
Date:
2015-11-26
Revision:
1:894dcc6f650d
Parent:
0:0745169668f2

File content as of revision 1:894dcc6f650d:

#include "mbed.h"
#include "Ticker.h"

#define marker 16.0

//***************************************************************************************************
//Outputs

DigitalOut led1(LED1);

DigitalOut led2(LED2);

DigitalOut led4(LED4);

DigitalOut Motor(p14);

PwmOut servo(p21);

//***************************************************************************************************
//Inputs

InterruptIn input(p17);



//***************************************************************************************************
//Communication

Serial pc(USBTX, USBRX); // tx, rx

//***************************************************************************************************
//Variables

float sample;

volatile float rpm = 0, rpm_total;

bool newDetection = true;   //need this as multiple reads happen across white spot

bool run_down = false;

bool broadcast = false;

bool broadcast_old = false;

int samples[10];           //higher number = greater accuracy but longer read time

int sampleCounts = 0, sampleCounts_old = 0;

bool Motor_status = false;



volatile int delta_t;


//***************************************************************************************************
//Misc

Timer t;

Ticker serial_tx;

int seconds = 0;

void rpm_counter()
{

    sampleCounts++;

    led2 = !led2;

}

void servo_engage_movement()
{

    servo.pulsewidth_us(1100 ); // servo position determined by a pulsewidth between 1-2ms

    //pc.printf("offset = %i us \n\r",offset);
}

void servo_disengage_movement()
{

    servo.pulsewidth_us(1900); // servo position determined by a pulsewidth between 1-2ms

    // pc.printf("offset = %i us \n\r",offset);

}


void rpm_display()
{

    rpm_total = sampleCounts/marker;

    rpm = ((rpm*4) + ((sampleCounts - sampleCounts_old)/ marker * 60))/5;

    sampleCounts_old = sampleCounts;

    if(run_down == false && broadcast == false && Motor_status == false) {

        servo_engage_movement();

        wait_ms(500);

        Motor = 0;

        Motor_status = true;

        sampleCounts = 0;

        sampleCounts_old = 0;

    }

    if(rpm > 505 && run_down == false) {

        servo_disengage_movement();

        Motor = 1;

        Motor_status = false;

        run_down = true;
    }

    if(run_down == true && rpm < 5) {

        run_down = false;

        broadcast = false;
    }

    if(run_down == true && rpm <= 500) {

        broadcast_old = broadcast;

        broadcast = true;
    }

if(broadcast != broadcast_old) {

        seconds = 0;

        //pc.printf("Beginn der Drehzahlaufzeichnung\n\r");
    }

    if(broadcast == true) {

        pc.printf("$1;1;%i;%f;0\r\n",seconds,rpm);
        
        ++seconds;
        
    }
        
    led1 = !led1;

}


int main(void)
{

    Motor = 1;

    Motor_status = false;

    servo.period(0.020);          // servo requires a 20ms period

    t.start(); //start initial timer

    servo.pulsewidth_us(2000);

    input.rise(&rpm_counter);

    serial_tx.attach(&rpm_display,1); // the function rpm_display attached to the ticker wich is using the interval of 1 second



    while(true) {



        led4 = !run_down;

    }
}