Inverted Pendulum / Mbed 2 deprecated IP-Interface

Dependencies:   mbed QEI

Revision:
20:8063c82bbb35
Parent:
12:65392ee77300
Child:
22:c18f04d1dc49
--- a/pot/pot.cpp	Wed Nov 16 03:44:19 2016 +0000
+++ b/pot/pot.cpp	Wed Nov 16 08:28:46 2016 +0000
@@ -1,18 +1,49 @@
 #include "mbed.h"
 #include "pot.h"
 
-Pot::Pot(PinName _pin, int offset) : angle(_pin) {
+
+Pot::Pot(PinName _pin, QEI* motor_encoder, float offset) : pend_angle(_pin),encoder() {
+    encoder=motor_encoder;
     offset=offset;
+    
+}
+
+void Pot::update(){
+    angular_velocity = (get_angle() - angle)/ UPDATE_TIME;
+    angle=get_angle();
+    velocity = (get_position() - position) / UPDATE_TIME;   
+    position=get_position();
+}
+    
+void Pot::set_zeros(){
+    this->offset = (-1)*this->get_angle();
+    this->encoder->reset();
 }
 
 float Pot::get_angle(){
-    float a = angle.read()*340; 
+    float a = pend_angle.read() * VOLTAGE_ANGLE_RATIO + offset; 
+    return a;  
+}
+
+float Pot::get_position(){
+    float a = (-1)*encoder->getPulses()*CM_PER_PULSE; 
+    return a;  
+}
+
+float Pot::angle_as_voltage(){
+    float a = pend_angle.read(); 
     return a;  
 }
-float Pot::get_voltage(){
-    float a = angle.read(); 
-    return a;  
+
+int Pot::position_as_pulse(){
+    return this->encoder->getPulses();  
 }
-void Pot::set_offset(int angle){
-     offset = angle; 
-}
\ No newline at end of file
+
+void Pot::print_test(){
+    printf("Position: %f \tVelocity: %f \tAngle: %f \tAngular Velocity: %f \r\n", position, velocity, angle, angular_velocity);     
+}
+
+
+
+
+