Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Multiplexer-Test by
Diff: Multiplexer_read.cpp
- Revision:
- 5:c89afbb7d0b2
- Parent:
- 4:57163b0e7dbc
- Child:
- 6:d61faae1989d
--- a/Multiplexer_read.cpp Mon Sep 08 18:21:48 2014 +0000 +++ b/Multiplexer_read.cpp Mon Sep 08 20:41:22 2014 +0000 @@ -1,8 +1,5 @@ #include "mbed.h" -#include "USBSerial.h" - -#include "EthernetInterface.h" //*************************************************************************************************** //Outputs @@ -13,23 +10,17 @@ DigitalOut timetest_0 (p30); DigitalOut timetest_1 (p29); -PwmOut Bremsenstrom_MOSFET(p21); -PwmOut Motroregler_PWM(p22); - +//PWMout Mosfet(p21); BusOut unused(p18); //*************************************************************************************************** //Inputs -InterruptIn Drehzahl_lichtschranke(p14); - -Timer Umlaufzeit; -Ticker ticker_read_sensor; AnalogIn Messkanal_0 (p19); AnalogIn Messkanal_1 (p20); -AnalogIn DMS_0 (p17); + //*************************************************************************************************** @@ -37,18 +28,8 @@ Serial pc(USBTX, USBRX); -UDPSocket udp; -Endpoint udp_dest; -float MOSFET_duty_cycle = 0; - -volatile float Motorspannung, Bremsenspannung , Motorstrom, Aux, Bremsenstrom, Temperatur_1, Temperatur_2, Temperatur_3 ; - -//int Motor_ratio = 0, curr_Motor_pwm_pulsewidth = 0,curr_MOSFET_pwm_pulsewidth = 0, Drehzahl = 0; - -volatile int Drehzeit_counter = 0; -#define DREHZEIT_SIZE 3 -volatile int Drehzeit[DREHZEIT_SIZE]; +volatile float Motorspannung, Bremsenspannung , Motorstrom, Aux, Bremsenstrom, Temperatur_1, Temperatur_2, Temperatur_3, c1v1, c1v2 ; //*************************************************************************************************** @@ -57,271 +38,85 @@ void interr_read_sensors() { - int Messwert_0 = 0, Messwert_1 = 0, select_0 = 0, select_1 = 0; + int Messwert_0 = 0, Messwert_1 = 0; Multiplex_select_0 = 0, Multiplex_select_1 = 0; //Selection of Multiplexer states - for (int i=0; i<=4; i++) { - //while(i<=4) { - select_0 = Multiplex_select_0; - select_1 = Multiplex_select_1; + for (int i=0; i<=4; i++) { + //while(i<=4) { - Messwert_0 = Messkanal_0.read_u16(); - Messwert_1 = Messkanal_1.read_u16(); - Multiplex_select_0 = i&0x01; Multiplex_select_1 = (i>>1)&0x01; wait(0.001); + + Messwert_0 = Messkanal_0.read_u16(); + Messwert_1 = Messkanal_1.read_u16(); switch(i) { case 0: { - Temperatur_1 = Messwert_0; + Multiplex_select_0 = 0; + Multiplex_select_1 = 0; + wait(0.001); + Temperatur_1 = Messwert_0 ; Motorspannung = Messwert_1 * 0.000515 - 0.05 ; //Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode + break; + } + case 1: { + Multiplex_select_0 = 1; + Multiplex_select_1 = 0; + wait(0.001); + Temperatur_2 = Messwert_0 ; + Motorstrom = Messwert_1 * 0.000396 - 4.15;// + break; + } + + case 2: { + Multiplex_select_0 = 0; + Multiplex_select_1 = 1; + wait(0.001); + Temperatur_3 = Messwert_0; + Bremsenspannung = Messwert_1 * 0.000515 - 0.05;//Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode break; } case 3: { - Aux = Messwert_0; - Bremsenstrom = Messwert_1; // * 0.00032 - 15.8; - - break; - } - case 1: { - Temperatur_2 = Messwert_0; - Motorstrom = Messwert_1 * 0.000396 - 4.15; + Multiplex_select_0 = 1; + Multiplex_select_1 = 1; + wait(0.001); + Aux = Messwert_0 / 1629; + Bremsenstrom = Messwert_1; //* 0.00032 - 15.8; break; } - case 2: { - Temperatur_3 = Messwert_0; - Bremsenspannung = Messwert_1 * 0.000515 - 0.05;//Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode - - break; - } - - /* default: { - //i =0; - break; */ - } - - /* Multiplex_select_0 = i&0x01; - Multiplex_select_1 = (i>>1)&0x01; - wait(0.001); - //i++;*/ } } //*************************************************************************************************** -//Control Motor rpm - -int rpm_control(float motor_n_cmd, float motor_n_cur) -{ - - static int motor_pwm_cmd_last = 900; - //static float motor_n_last = 0; - - if (motor_n_cmd < 1.0) { - Motroregler_PWM.pulsewidth_us(900); - motor_pwm_cmd_last = 900; -// motor_n_last = 0; - return 1; - } - - float motor_n_dif = motor_n_cmd - motor_n_cur; - - int motor_pwm_cmd = (int)(motor_pwm_cmd_last + motor_n_dif * 0.6 + 0.5); // round() ... works only for positive values - - pc.printf("cmd: %7.2f, cur: %7.2f, dif: %7.2f, motor_pwm_cmd: %4d, motor_pwm_dif: %4d, DMS: %f\n\r", - motor_n_cmd*60, motor_n_cur*60, motor_n_dif*60, motor_pwm_cmd, motor_pwm_cmd-motor_pwm_cmd_last, ((int)DMS_0.read_u16())/65536.0*3.3); - - if (motor_pwm_cmd > 1900) motor_pwm_cmd = 1900; - else if (motor_pwm_cmd < 1010) motor_pwm_cmd = 1005; - - Motroregler_PWM.pulsewidth_us(motor_pwm_cmd); - motor_pwm_cmd_last = motor_pwm_cmd; - - return 1; - -} - - -//*************************************************************************************************** -//Control MOSFET pwm -int brk_mosfet_control(float mosfet_pwm_cmd, float mosfet_pwm_cur) -{ - - static int mosfet_pwm_cmd_last = 0; - - - if (mosfet_pwm_cmd < 1.0) { - Bremsenstrom_MOSFET.pulsewidth_us(00); - mosfet_pwm_cmd_last = 0; +// MAIN: - return 1; - } - - float mosfet_pwm_dif = mosfet_pwm_cmd - mosfet_pwm_cur; - - // int mosfet_pwm_cmd = (int)(mosfet_pwm_cmd_last + mosfet_pwm_dif * 0.6 + 0.5); // round() ... works only for positive values - - pc.printf("cmd: %7.2f, cur: %7.2f, dif: %7.2f, mosfet_pwm_cmd: %4d, mosfet_pwm_dif: %4d", - mosfet_pwm_cmd, mosfet_pwm_cur, mosfet_pwm_dif, mosfet_pwm_cmd, mosfet_pwm_cmd-mosfet_pwm_cmd_last); - - if (mosfet_pwm_cmd > 20000) mosfet_pwm_cmd = 20000; - else if (mosfet_pwm_cmd < 0) mosfet_pwm_cmd = 0; - - Bremsenstrom_MOSFET.pulsewidth_us(mosfet_pwm_cmd); - - mosfet_pwm_cmd_last = mosfet_pwm_cmd; - - return 1; -} - - -//*************************************************************************************************** -//Calculate rpm -void Motor_drehzahl() -{ - static bool first_run = true; -// Umlaufzeit.stop(); - int tmp = Umlaufzeit.read_us(); - if (first_run) { - Umlaufzeit.start(); - first_run = false; - return; - } - if (tmp < 1000) return; - - // Cache last 3 values for averaging - Drehzeit[Drehzeit_counter % DREHZEIT_SIZE] = tmp; - ++Drehzeit_counter; - Umlaufzeit.reset(); -} - - -//*************************************************************************************************** -// MAIN: int main(void) { - /* - // Init networking - EthernetInterface eth; - int err = eth.init(); - if (err) - pc.printf("eth.init() failed. (%d)\n\r", err); - else - pc.printf("eth.init() successful.\n\r"); - err = eth.connect(); - if (err) - pc.printf("eth.connect() failed. (%d)\n\r", err); - else - pc.printf("eth.connect() successful.\n\r"); - - char *ip_address = eth.getIPAddress(); - pc.printf("IP-Address: %s\n\r", ip_address); - - err = udp.init(); - if (err) - pc.printf("udp.init() failed. (%d)\n\r", err); - else - pc.printf("udp.init() successful.\n\r"); - - err = udp_dest.set_address("192.168.0.183", 1234); - if (err) - pc.printf("udp_dest.set_address() failed. (%d)\n\r", err); - else - pc.printf("udp_dest.set_address() successful.\n\r"); - */ - - // Init PWM - Bremsenstrom_MOSFET.period(0.020); - Motroregler_PWM.period(0.020); - Motroregler_PWM.pulsewidth_us(900); - - wait(1.0); - - Drehzahl_lichtschranke.fall(&Motor_drehzahl); - ticker_read_sensor.attach(&interr_read_sensors, 1.0); - - // Time counters - Timer timer_print, timer_pwm; - timer_print.start(); - timer_pwm.start(); - - float motor_rpm_cmd = 0; - float motor_n_cur = 0; - - pc.printf("Enter motor rpm to begin\n\r"); while(true) { - /* - - if (pc.readable()) { - pc.printf("\n\r Motor rpm\n\r"); - pc.scanf("%f",&motor_rpm_cmd); - pc.printf("%f\n\r",motor_rpm_cmd); - - } - - // Calculate motor_n_cur by averaging - int drehzeit_sum = 0; - for (int i=0; i != DREHZEIT_SIZE; ++i) - drehzeit_sum += Drehzeit[i]; - - motor_n_cur = (drehzeit_sum ? (1.0e6/drehzeit_sum)*DREHZEIT_SIZE : 0.0); - - - // Set motor_n_cur to 0 if the interrupt wasn't called for a specified time - if (Umlaufzeit.read_ms() > 200) - motor_n_cur = 0.0; - - - // Controller is only called every n ms - if (timer_pwm.read_ms() > 500) { - // pc.printf("drehzeit_sum: %d, motor_n_cur: %f\n\r", drehzeit_sum, motor_n_cur); - // pc.printf("%d %d %d\n\r", Drehzeit[0], Drehzeit[1], Drehzeit[2]); - //timetest_0 = 1; + interr_read_sensors(); + + + Multiplex_select_0 = 1, Multiplex_select_1 = 0; + pc.printf(" [Temperatur1, Motorspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_1, Motorspannung); + pc.printf(" [Temperatur2, Motorstrom]; 0 %5.2f deg; 1 %5.2f A;", Temperatur_2, Motorstrom); + pc.printf(" [Temperatur3, Bremsenspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_3, Bremsenspannung); + pc.printf(" [Aux, Bremsenstrom]; 0 %5.2f ; 1 %5.2f ;", Aux, Bremsenstrom); + pc.printf(" \n\r\n\r"); + wait(1); - timer_pwm.reset(); - rpm_control(motor_rpm_cmd/60.0, motor_n_cur); - // timetest_0 = 0; - } - */ - - - // Myled = 0; - - // wait (0.5); - - - - if (timer_print.read_ms() > 1000) { - timer_print.reset(); - - pc.printf(" [Temperatur1, Motorspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_1, Motorspannung); - pc.printf(" [Temperatur2, Motorstrom]; 0 %5.2f deg; 1 %5.2f A;", Temperatur_2, Motorstrom); - pc.printf(" [Temperatur3, Bremsenspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_3, Bremsenspannung); - pc.printf(" [Aux, Bremsenstrom]; 0 %5.2f NA; 1 %5.2f A;", Aux, Bremsenstrom); - pc.printf(" \n\r\n\r"); - - float drehzahl = 1;//(Drehzeit ? 1.0e6/Drehzeit : 0.0); - - /* pc.printf("\n\rCounter: %d, Drehzahl: %f rpm (%f Hz)\n\r\n\r", Drehzeit_counter, drehzahl*60, drehzahl); - - char udb_buf[] = "Tescht"; - err = udp.sendTo(udp_dest, udb_buf, sizeof(udb_buf)); - if (err == -1) - pc.printf("udp.sendTo() failed. (%d)\n\r", err); - */ -} } } \ No newline at end of file