it works. Do you have any idea why it doesn’t work when I put it in my main code ?
#include “mbed.h”
#include
#include
#include
// Parameter des Einspritzsignals
AnalogIn t_oefnen(PA_3);
AnalogIn t_einspritzen(PC_0);
DigitalIn BM1_BM2(PB_8); // Auswahl des Betriebsmodus
AnalogIn KW_position(PC_3);
// Ausgang des Einspritzsignals
PwmOut einspritzung(PE_11);
// Parameter zur Pumpensteuerung
AnalogIn frequenz(PF_3);
AnalogIn tastgrad(PF_5);
PwmOut pwm_pumpe(PB_9); // Ausgang zur Pumpenstueuerung
// Kurbelwellensignal
PwmOut myPWM1(PD_13);
// Nockemwellensignal
PwmOut myPWM2(PA_0);
// test area
DigitalOut led(LED1);
const int teeth_count = 80; // Gesamtzahl der Zähne
volatile float time_between_edges = 0.0f;
const int Winkel_offset = 47; // Gesamtzahl der Zähne
class Counter {
public:
Counter(PinName pin)
: _interrupt(
pin) { // create the InterruptIn on the pin specified to Counter
_interrupt.fall(
callback(this, &Counter::increment)); // attach increment function of
// this counter instance
}
void increment() { _count++; }
int read() { return _count; }
void reset() { _count = 0; }
private:
InterruptIn _interrupt;
volatile int _count;
};
Counter counter1(PF_14);
Counter counter2(PF_15);
int main() {
BM1_BM2.mode(PullUp);
//KW-Signal
myPWM1.period(1.0 / 10000.0f);
myPWM1.write(0.5f);
//NW-Signal
myPWM2.period(1.0 / 10.0f);
myPWM2.write(0.5f);
Timer t1; // create a Timer object
t1.start(); // start the timer
Timer t2; // create a Timer object
t2.start(); // start the timer
int prev_count = 0;
// int prev_count = 0;
while (1) {
// Potisdaten auslesen:
float pot01_value, pot02_value, pot03_value, pot04_value, pot05_value,
wait_time01, wait_time02, wait_time03;
pot01_value = t_oefnen.read();
wait_time01 = pot01_value * 10.0f;
pot02_value = t_einspritzen.read();
wait_time02 = pot02_value * 10.0f;
pot03_value = KW_position.read();
wait_time03 = pot03_value * 10.0f;
// Pumpensteuerung
pot04_value = 1.0 / (frequenz.read() * 1000.0);
pot05_value = tastgrad.read();
pwm_pumpe.period(pot04_value);
pwm_pumpe.write(pot05_value);
if (BM1_BM2 == true) {
for (int i = 0; i <= 1000; i++) {
//printf(“Starting the loop\n”);
if (BM1_BM2 == false) {
break; // stop the loop if BM1_BM2 is false
}
led = 1;
// read and process t_oefnen
einspritzung.write(1);
wait_us(wait_time01 * 1000);
// read and process t_einspritzen
einspritzung.period(1.0f / 15000.0f);
einspritzung.write(0.5f);
wait_us(wait_time02 * 1000);
// read and process t_wiederholung
einspritzung.write(0);
printf("wait_time01: %f \t wait_time02: %f \n", wait_time01*1000, wait_time02*1000);
}
} else {
led = 0;
int count1 = counter1.read();
float period = std::chrono::duration<float>{t1.elapsed_time()}
.count(); // get the elapsed time since start
float rpm = ((count1 / period) * 60.0) /
teeth_count; // Drehzahl in U/min berechnen
int count2 = counter2.read();
if (count2 > prev_count) { // Second edge detected // read and
// process t_oefnen
wait_us(wait_time03 * 1000);
einspritzung.write(1);
wait_us(wait_time01 * 1000);
// read and process t_einspritzen
einspritzung.period(1.0f / 15000.0f);
einspritzung.write(0.5f);
wait_us(wait_time02 * 1000);
// read and process t_wiederholung
einspritzung.write(0);
time_between_edges =
std::chrono::duration<float>{t2.elapsed_time()}.count();
// printf("Elapsed time: %lld s\n", t.elapsed_time().count());
t2.stop(); // Stop the timer to prevent it from continuing to run in
// the background
t2.reset(); // Reset the timer for the next measurement
t2.start(); // Start the timer again for the next measurement
}
printf("count1: %d \t prev_count: %d \t count2: %d \n", count1,prev_count, count2 );
prev_count = count2;
t1.reset(); // reset the timer
counter1.reset();
counter2.reset();
ThisThread::sleep_for(1000ms);
}
}
}