ti bisogna il phaserunner

Dependencies:   mbed PID mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
beacon
Date:
Tue Jun 04 19:03:39 2019 +0000
Parent:
10:b7a44c4a4ef6
Commit message:
ti bisogna il phaserunner

Changed in this revision

PID.lib Show annotated file Show diff for this revision Revisions of this file
Peripherien/Phaserunner.cpp Show annotated file Show diff for this revision Revisions of this file
Peripherien/Phaserunner.h Show annotated file Show diff for this revision Revisions of this file
Peripherien/Regler.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r b7a44c4a4ef6 -r 39bd79605827 PID.lib
--- a/PID.lib	Wed May 29 18:57:39 2019 +0000
+++ b/PID.lib	Tue Jun 04 19:03:39 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/EpicG10/code/PID/#7162a5db5417
+https://os.mbed.com/users/beacon/code/PID/#d80b407260e9
diff -r b7a44c4a4ef6 -r 39bd79605827 Peripherien/Phaserunner.cpp
--- a/Peripherien/Phaserunner.cpp	Wed May 29 18:57:39 2019 +0000
+++ b/Peripherien/Phaserunner.cpp	Tue Jun 04 19:03:39 2019 +0000
@@ -1,11 +1,21 @@
 #include "Phaserunner.h"
 
-Phaserunner::Phaserunner(RawSerial& connection): connection(connection), led(LED2){
-    //this->connection = connection;
+Phaserunner::Phaserunner(RawSerial& connection):
+connection(connection),
+led(LED2),
+analogOut(NULL),
+PHASERUNNERTYPE(MOTORS){
     this->connection.attach(callback(this, &Phaserunner::Rx_interrupt), Serial::RxIrq);
+    this->ticker.attach(callback(this, &Phaserunner::reduce_timer), 0.001f);
+}
 
-    //this->thread.start(callback(this, &Phaserunner::writeToPhaserunner));
-    //this->ticker.attach(callback(this, &Phaserunner::writeToPhaserunner), 0.5f);
+Phaserunner::Phaserunner(RawSerial &connection, AnalogOut *analogOut):
+connection(connection),
+led(LED2),
+analogOut(analogOut),
+PHASERUNNERTYPE(PEDALS){
+    this->connection.attach(callback(this, &Phaserunner::Rx_interrupt), Serial::RxIrq);
+    this->ticker.attach(callback(this, &Phaserunner::reduce_timer), 0.001f);
 }
 
 int Phaserunner::WriteRegister(uint8_t *buf, unsigned short addr, unsigned short value){
@@ -90,7 +100,7 @@
 
     return crcRegister;
 }
-void Phaserunner::writeToPhaserunner(){
+/*void Phaserunner::writeToPhaserunner(){
     //while( true ){
     //RPM
     this->readRPM();
@@ -104,7 +114,7 @@
     this->writeRecuperation(this->newRecuperation);
     wait_ms(WRITE_PERIOD);
     //}
-}
+}*/
 void Phaserunner::setTorque(uint8_t torque){
     if( torque > 100 ){
         torque = 100;
@@ -114,22 +124,33 @@
     } else {
         this->newTorque = torque;
     }
-    //this->writeTorque(torque);
+    switch( PHASERUNNERTYPE ){
+        case PEDALS: this->analogTorque(newTorque); break;
+        case MOTORS: this->writeTorque(newTorque);  break;
+    }
 }
 void Phaserunner::setRecuperation(uint8_t recuperation){
     this->newRecuperation = recuperation > 100 ? 100 : recuperation;
 }
 void Phaserunner::writeTorque(uint8_t torque){
     unsigned short value = (torque * 4096) / 100;
+    wait_ms(timer);
     this->sendBuffer(REMOTE_THROTTLE_VOLTAGE, value);
+    timer = WRITE_PERIOD;
+}
+void Phaserunner::analogTorque(uint8_t torque){
+    analogOut->write(torque * 0.01f);
 }
 void Phaserunner::writeRecuperation(uint8_t recuperation){
     unsigned short value = (recuperation * 4096) / 100;
+    wait_ms(timer);
     this->sendBuffer(REMOTE_ANALOG_BREAK_VOLTAGE, value);
+    timer = WRITE_PERIOD;
 }
 void Phaserunner::readRPM(){
-    //TODO: Check how registers are read...
+    wait_ms(timer);
     this->readBuffer(MOTOR_CURRENT);
+    timer = WRITE_PERIOD;
 }
 
 void Phaserunner::Rx_interrupt(){
@@ -220,10 +241,14 @@
 
 int Phaserunner::getRegister(int address){
 
- readRegister(this->read_buffer,address);
+    readRegister(this->read_buffer,address);
     return this->read_buffer[2]+this->read_buffer[3]<<8;
 }
 
 uint16_t Phaserunner::getRecup(){
     return this->newRecuperation;
+}
+
+void Phaserunner::reduce_timer(){
+    if( timer ) timer--;
 }
\ No newline at end of file
diff -r b7a44c4a4ef6 -r 39bd79605827 Peripherien/Phaserunner.h
--- a/Peripherien/Phaserunner.h	Wed May 29 18:57:39 2019 +0000
+++ b/Peripherien/Phaserunner.h	Tue Jun 04 19:03:39 2019 +0000
@@ -23,14 +23,20 @@
     //Modbus-Protokoll
     static const uint8_t SCHREIBANTWORT =   0x10;
     static const uint8_t LESEANTWORT =      0x03;
-    
+
+    //Pedale oder Motor
+    const uint8_t PHASERUNNERTYPE;
 
     //Sonstiges
     static const uint16_t WRITE_PERIOD =    3;      //Minimaler Zeitintervall zwischen zwei Messages an den Phaserunner [ms]
     static const float TICKER_PERIOD =      0.02f;  //Zeitintervall für das Schreiben auf den Phaserunner [s]
-    
+
+    AnalogOut* analogOut;   //Wenn der Phaserunner Pedale kontrolliert, hat er einen analogOut
+
     //Thread thread; //Wird für die Funktion writeToPhaserunner() gebraucht
-    Ticker ticker;
+
+    Ticker ticker;      //Wird gebraucht um nicht zu häufig zu schreiben
+    uint8_t timer;       //Zeit, die vergehen muss, bis wieder geschrieben werden kann [ms]
 
     //Verbindung zum Phaserunner
     RawSerial& connection;
@@ -75,7 +81,7 @@
      * @param value:    Wert der in das Register geschriebern werden soll.
      * @return          Anzahl gesendeter Bytes
      */
-    //int sendBuffer(unsigned short adress, unsigned short value);
+    int sendBuffer(unsigned short adress, unsigned short value);
 
     /**
      * @brief   Sendet den writeBuffer
@@ -89,7 +95,7 @@
      * @param adress:   Adresse des Registers aus dem gelesen werden soll.
      * @return:         Anzahl gelesener Bytes
      */
-    //int readBuffer(uint16_t adress);
+    int readBuffer(uint16_t adress);
 
     /**
      * @brief:  Schreibt ein Drehmoment auf den Phaserunner.
@@ -98,6 +104,12 @@
     void writeTorque(uint8_t torque);
 
     /**
+     * @brief Schreibt einen Analogwert mit dem das Drehmoment kontrolliert wird.
+     * @param torque
+     */
+    void analogTorque(uint8_t torque);
+
+    /**
      * @brief:  Schreibt den Rekuperationswert auf den Phaserunner.
      * @param:  recuperation Rekupertation in Prozent
      */
@@ -111,9 +123,9 @@
     static uint16_t getCRC(uint8_t* msgByte, uint8_t length);
 
     /**
-     * @brief: Sendet kontinuierlich einen Lesebefehl auf den Phaserunner.
+     * @brief: Sendet Befehle dem Phaserunner sobald es möglich ist.
      */
-    void writeToPhaserunner();
+    //void writeToPhaserunner();
 
     /**
      * @brief:  Sendet einen Schreibbefehl für die Drehgewschwindigkeit, die Stromstärke und die Spannung
@@ -126,12 +138,21 @@
      */
     uint16_t readFaults();
 
+    /**
+     * Reduziert den timer (Wird mit dem Ticker aufgerufen).
+     */
+    void reduce_timer();
+
 public:
     static const uint8_t MAX_TORQUE_GAIN    = 2;    //Maximaler Sprung für Drehmoment
     static const uint8_t MIN_RECUPERATION   = 10;   //Schwellwert für die Aktivierung des Daumengriffbetätigers
     static const uint8_t MIN_HANDGRIFF      = 5;    //Schwellwert für die Aktivierung des Handgriffbetätigers
-    int sendBuffer(unsigned short adress, unsigned short value);
-    int readBuffer(uint16_t adress);
+
+    static const uint8_t PEDALS             = 0;    //Phaserunner Type Pedal
+    static const uint8_t MOTORS             = 1;    //Phaserunner Type Motor
+
+    //int sendBuffer(unsigned short adress, unsigned short value);
+    //int readBuffer(uint16_t adress);
     /**
      * @brief Initialisiert ein Phaserunner-Objekt
      * @param connection: Serielle Schnittstelle zum Phaserunner
@@ -139,6 +160,13 @@
     Phaserunner(RawSerial& connection);
 
     /**
+     * Konstruktor für Pedale
+     * @param connection
+     * @param analogOut
+     */
+    Phaserunner(RawSerial& connection, AnalogOut* analogOut);
+
+    /**
      * @brief:  Schreibt ein Drehmoment ins Phaserunnerobjekt, das dann geschrieben wird.
      * @param:  torque Drehmoment in Prozent
      */
@@ -164,7 +192,7 @@
      * @return spannung
      */
     float getVoltage();
-    
+
     /**
      * @return Ebrike Source
      */
@@ -175,7 +203,7 @@
     uint8_t bufPointer;
     uint8_t read;
     DigitalOut led;
-    
+
     uint16_t getRecup();
 };
 
diff -r b7a44c4a4ef6 -r 39bd79605827 Peripherien/Regler.cpp
--- a/Peripherien/Regler.cpp	Wed May 29 18:57:39 2019 +0000
+++ b/Peripherien/Regler.cpp	Tue Jun 04 19:03:39 2019 +0000
@@ -107,13 +107,13 @@
     bool init = false;
     while(!encoder.reset()){
         if(!init){
-           rechtspedal.sendBuffer(495,0.2*4096); // Setzt für kurze Zeit einen grosseren Moment
+           rechtspedal.setTorque(2); // Setzt für kurze Zeit einen grosseren Moment
            init = true;
            wait(0.005);
-           rechtspedal.sendBuffer(495,0.03*4096);// Setzt für den Rest der Zeit einen kleinen Moment
+           rechtspedal.setTorque(3);// Setzt für den Rest der Zeit einen kleinen Moment
         }
     }
-    rechtspedal.sendBuffer(495,0);               // Setzt Moment auf Null nach dem Reset
+    rechtspedal.setTorque(0);               // Setzt Moment auf Null nach dem Reset
     wait(0.1);
 }
 
@@ -164,12 +164,12 @@
     
     // Hilfemoment für Rücktritt steuern
     if(F_RPM < -5.0f && !richtung1){
-            rechtspedal.sendBuffer(495,0.03*4096);
+            rechtspedal.setTorque(0.03*4096);
             richtung1 = true;
             richtung2 = false;
     }
     else if(F_RPM > -3.0f && !richtung2){
-            rechtspedal.sendBuffer(495,0);
+            rechtspedal.setTorque(0);
             richtung1 = false;
             richtung2 = true;
     }
diff -r b7a44c4a4ef6 -r 39bd79605827 main.cpp
--- a/main.cpp	Wed May 29 18:57:39 2019 +0000
+++ b/main.cpp	Tue Jun 04 19:03:39 2019 +0000
@@ -78,10 +78,10 @@
     pc.printf("Siamo accesi\n\r");
     wait(1);
     
-    linkspedal.sendBuffer(495,0);
-    rechtspedal.sendBuffer(495,0);
-    vorderrad.sendBuffer(495,0);
-    hinterrad.sendBuffer(495,0);
+    linkspedal.setTorque(0);
+    rechtspedal.setTorque(0);
+    vorderrad.setTorque(0);
+    hinterrad.setTorque(0);
     wait(0.2);
     
     // Pedelec Values
@@ -117,7 +117,7 @@
     
     // PID instance
     float Kp = 1.2, Ki = 40, Kd = 0.0;
-    PID pid(Kp, Ki, Kd, dt);
+    PID pid(Kp, Ki, Kd, dt, true);
     pid.setInputLimits(0.0,200.0);
     pid.setOutputLimits(1.0,100.0);
     pid.setMode(1); //Regulator
@@ -134,13 +134,13 @@
     
     while(!encoder.reset()){
         if(!init){
-           rechtspedal.sendBuffer(495,0.8*4096);
+           rechtspedal.setTorque(80);
            init = true;
            wait(0.04);
-           rechtspedal.sendBuffer(495,0.03*4096);
+           rechtspedal.setTorque(3);
         }
     }
-    rechtspedal.sendBuffer(495,0);
+    rechtspedal.setTorque(0);
     wait(0.1); 
     init = false;
      float leistungsfaktor = 4.8; // Faktor zwischen Elektrischeleistung und mechanische Leistung %
@@ -216,12 +216,12 @@
             
             // Hilfemoment für Rücktritt steuern
             if(F_RPM < -5.0f && !richtung1){
-                rechtspedal.sendBuffer(495,0.03*4096);
+                rechtspedal.setTorque(3);
                 richtung1 = true;
                 richtung2 = false;
             }
             else if(F_RPM > -3.0f && !richtung2){
-                rechtspedal.sendBuffer(495,0);
+                rechtspedal.setTorque(0);
                 richtung1 = false;
                 richtung2 = true;
             }
@@ -234,14 +234,14 @@
             if(F_RPM > 1.0f && !Bremsen && !recup){
                 torque = (F_PMech / 300.0 * 100)*pedFaktor;
                 if(i%10 == 0 && (torque < (torqueOld*0.95) || torque > (torqueOld * 1.05))) {
-                    vorderrad.sendBuffer(497,0);
-                    hinterrad.sendBuffer(497,0); 
+                    vorderrad.setRecuperation(0);
+                    hinterrad.setRecuperation(0); 
                     // write torque to phaserunner;
-                    //vorderrad.sendBuffer(477,uint16_t((80+(torque))*40.96f));
-                    vorderrad.sendBuffer(495,uint16_t((0.10+(torque/100.0f))*4096));
-                    //hinterrad.sendBuffer(495,uint16_t((0.20+(torque/100.0f))*4096));
-                    //vorderrad.sendBuffer(495,0);
-                    hinterrad.sendBuffer(495,0);
+                    //vorderrad.setRecuperation(uint16_t((80+(torque))*40.96f));
+                    vorderrad.setTorque(uint16_t(10+torque));
+                    //hinterrad.setTorque(uint16_t(20+torque));
+                    //vorderrad.setTorque(0);
+                    hinterrad.setTorque(0);
                     write = 10;
                 torqueOld = torque;
                 printf("State: torque\n\r");
@@ -256,18 +256,18 @@
                writeNull = true;
                write = -10;
                // write 0 to phaserunner
-               vorderrad.sendBuffer(495,0);
-               hinterrad.sendBuffer(495,0);
+               vorderrad.setTorque(0);
+               hinterrad.setTorque(0);
                
             }
             else write = 0; 
             
             if((daumen.getValue() > Phaserunner::MIN_RECUPERATION)&& !recup){
-               vorderrad.sendBuffer(495,0);
-               hinterrad.sendBuffer(495,0);
+               vorderrad.setTorque(0);
+               hinterrad.setTorque(0);
                wait_ms(5);
-               vorderrad.sendBuffer(497,uint16_t((float)daumen.getValue()/100.0f*4096));
-               hinterrad.sendBuffer(497,uint16_t((float)daumen.getValue()/100.0f*4096));
+               vorderrad.setRecuperation(daumen);
+               hinterrad.setRecuperation(daumen);
                printf("State: recup\n\r");
                recup = true;
             }
@@ -277,11 +277,11 @@
             if(Bremsen && !gebremst){
                write = -5;
                // write 0 to phaserunner
-               vorderrad.sendBuffer(495,0);
-               hinterrad.sendBuffer(495,0);
+               vorderrad.setTorque(0);
+               hinterrad.setTorque(0);
                wait_ms(5);
-               vorderrad.sendBuffer(497,uint16_t(0.10f*4096));
-               hinterrad.sendBuffer(497,uint16_t(0.10f*4096));
+               vorderrad.setRecuperation(10);
+               hinterrad.setRecuperation(10);
                printf("State: bremsen\n\r");
                gebremst = true; 
             }
@@ -296,7 +296,7 @@
             float u;
             if(i%200 == 0){
                 i = 0;
-                vorderrad.readBuffer(262);
+                //vorderrad.readBuffer(262);
                 u = vorderrad.getVoltage();
                 printf("P=%.3f, Torque=%.3f , RPM_Ped =%.3f, U=%.2f\n\r",F_PMech,torque,F_RPM,u);
                 //printf("Daumen1: %d, Daumen2: %.2f\n\r",wert1,wert2);