Multiplexer lauffähig

Dependencies:   USBDevice mbed

Fork of Multiplexer-Test by H2M Teststand

Files at this revision

API Documentation at this revision

Comitter:
O_Shovah
Date:
Mon Sep 08 12:11:21 2014 +0000
Child:
1:84c53305b6a5
Child:
8:fd403cfcaa0a
Commit message:
Test;

Changed in this revision

Librarys.lib Show annotated file Show diff for this revision Revisions of this file
Multiplexer_read.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Librarys.lib	Mon Sep 08 12:11:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#74480b0b4033
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Multiplexer_read.cpp	Mon Sep 08 12:11:21 2014 +0000
@@ -0,0 +1,371 @@
+#include "mbed.h"
+
+#include "USBSerial.h"
+
+#include "EthernetInterface.h"
+
+//Outputs
+
+DigitalOut Multiplex_select_0 (p15);
+DigitalOut Multiplex_select_1 (p16);
+DigitalOut Myled (LED1);
+
+PwmOut Bremsenstrom_MOSFET(p21);
+PwmOut Motroregler_PWM(p22);
+
+
+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);
+
+
+
+//Communication
+
+Serial pc(USBTX, USBRX);
+
+UDPSocket udp;
+Endpoint udp_dest;
+
+float MOSFET_duty_cycle = 0;
+
+volatile float  Motorspannung, Bremsenspannung , Motorstrom, Aux,Bremsenstrom, Temperatur_0, Temperatur_1, Temperatur_2 ;
+
+//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];
+
+
+/*
+int Read_sensors(void)
+{
+
+    int  select_0 = 0, select_1 = 0, Messwert_0, Messwert_1,i = 0;
+
+    Multiplex_select_0 = 0, Multiplex_select_1 = 0;
+
+    //Selection of Multiplexer states
+
+
+//  for(i=0; i<=3; i++)
+
+    while(i<4)
+
+    {
+        select_0 = Multiplex_select_0;
+        select_1 = Multiplex_select_1;
+
+        Messwert_0 = Messkanal_0.read_u16();
+        Messwert_1 = Messkanal_1.read_u16();
+
+        switch(i) {
+            case 0: {
+                Temperatur_0 = Messwert_0;
+                Motorspannung = Messwert_1 * 0.000515 - 0.05 ; //Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode
+                pc.printf(" [%i,%i]; 0 %5.2f deg; 1 %5.2f V;", select_0, select_1, Temperatur_0, Motorspannung);
+                break;
+            }
+
+            case 1: {
+                Temperatur_1 = Messwert_0;
+                Motorstrom = Messwert_1 * 0.000396 - 4.15;
+                pc.printf(" [%i,%i]; 0 %5.2f deg; 1 %5.2f A;", select_0, select_1, Temperatur_1, Motorstrom);
+                break;
+            }
+
+            case 2: {
+                Temperatur_2 = Messwert_0;
+                Bremsenspannung = Messwert_1 * 0.000515 - 0.05;//Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode
+                pc.printf(" [%i,%i]; 0 %5.2f deg; 1 %5.2f V;", select_0, select_1, Temperatur_2, Bremsenspannung);
+                break;
+            }
+
+            case 3: {
+                Aux = Messwert_0;
+                Bremsenstrom = Messwert_1;// * 0.00032 - 15.8;
+                pc.printf(" [%i,%i]; 0 %5.2f NA; 1 %5.2f A;", select_0, select_1, Aux, Bremsenstrom);
+                break;
+            }
+
+            default: {
+                //i =0;
+                pc.printf("\n\r");
+                break;
+            }
+
+
+
+        }
+
+        wait(0.001);
+        i++;
+        Multiplex_select_0 = i&0x01;
+        Multiplex_select_1 = (i>>1)&0x01;
+
+    }
+
+    return 1;
+}
+*/
+
+void interr_read_sensors()
+{
+
+    int  Messwert_0, Messwert_1,select_0 = 0, select_1 = 0;
+
+    Multiplex_select_0 = 0, Multiplex_select_1 = 0;
+
+    //Selection of Multiplexer states
+
+
+    for (int i=0; i!=5; ++i)
+
+    {
+        select_0 = Multiplex_select_0;
+        select_1 = Multiplex_select_1;
+
+        Messwert_0 = Messkanal_0.read_u16();
+        Messwert_1 = Messkanal_1.read_u16();
+
+        switch(i) {
+            case 0: {
+                Temperatur_0 = Messwert_0;
+                Motorspannung = Messwert_1 * 0.000515 - 0.05 ; //Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode
+                
+                break;
+            }
+
+            case 1: {
+                Temperatur_1 = Messwert_0;
+                Motorstrom = Messwert_1 * 0.000396 - 4.15;
+                break;
+            }
+
+            case 2: {
+                Temperatur_2 = 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;
+            }
+
+            default: {
+                //i =0;
+                break;
+            }
+
+        }
+
+        
+        Multiplex_select_0 = i&0x01;
+        Multiplex_select_1 = (i>>1)&0x01;
+        wait(0.001);
+    }
+    
+}
+
+
+
+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;
+    
+    //    Bremsenstrom_MOSFET.pulsewidth_us(curr_MOSFET_pwm_pulsewidth);
+    Motroregler_PWM.pulsewidth_us(motor_pwm_cmd);
+    motor_pwm_cmd_last = motor_pwm_cmd;
+
+    return 1;
+
+}
+
+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) {
+        Motroregler_PWM.pulsewidth_us(00);
+        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;
+    
+    //    Bremsenstrom_MOSFET.pulsewidth_us(curr_MOSFET_pwm_pulsewidth);
+    Motroregler_PWM.pulsewidth_us(motor_pwm_cmd);
+    motor_pwm_cmd_last = motor_pwm_cmd;
+
+    return 1;
+}
+
+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();
+}
+
+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]);
+            
+            timer_pwm.reset();
+            rpm_control(motor_rpm_cmd/60.0, motor_n_cur);
+        }
+
+        // Myled = 0;
+
+        //  wait (0.5);
+        
+
+            
+        
+/*
+        if (timer_print.read_ms() > 1000) {
+            timer_print.reset();
+
+            pc.printf(" [%Temperatur0, Motorspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_0, Motorspannung);
+            pc.printf(" [%Temperatur1, Motorstrom]; 0 %5.2f deg; 1 %5.2f A;", Temperatur_1, Motorstrom);
+            pc.printf(" [%Temperatur0, Bremsenspannung]; 0 %5.2f deg; 1 %5.2f V;", Temperatur_2, Bremsenspannung);
+            pc.printf(" [Aux, Bremsenstrom]; 0 %5.2f NA; 1 %5.2f A;", Aux, Bremsenstrom);
+            
+            float drehzahl = (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[] = "123";
+            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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Sep 08 12:11:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file