ti bisogna il phaserunner

Dependencies:   mbed PID mbed-rtos

Revision:
11:39bd79605827
Parent:
9:56aed8c6779f
--- 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