Control project for the Lift-arm. Works with ROS Melodic

Dependencies:   mbed Servo ros_lib_melodic ULN2003_StepperDriver Async_4pin_Stepper

Revision:
0:441289ea4e29
Child:
1:7c355adbc977
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/encoder.h	Thu May 27 18:36:23 2021 +0000
@@ -0,0 +1,92 @@
+#ifndef KARBOT_ENCODER_H
+#define KARBOT_ENCODER_H
+
+/* Karbot encoder class
+ *
+ * This class is based upon the QEI class by Aaron Berk and the encoder class
+ * I wrote during ESP in 2nd year
+ *
+ * Written by Simon Krogedal
+ * 27/05/21
+ * Team 9 4th Year project
+ * 
+ * for NUCLEO-F401RE
+ * 
+ */
+ 
+#include "mbed.h"
+ 
+#define PREV_MASK 0x1 //Mask for the previous state in determining direction
+//of rotation.
+#define CURR_MASK 0x2 //Mask for the current state in determining direction
+//of rotation.
+#define INVALID   0x3 //XORing two states where both bits have changed.
+
+
+
+class encoder {
+    
+    private:
+        int         tot_clicks, temp_tot;   // clicks since last distance reset
+        double      click_rate, click_store;// clickrate
+        bool        c_d;                    // left or right bool
+        
+        /**
+         * Update the pulse count.
+         *
+         * Called on every rising/falling edge of channels A/B.
+         *
+         * Reads the state of the channels and determines whether a pulse forward
+         * or backward has occured, updating the count appropriately.
+         */
+        void encode(void);
+    
+        InterruptIn channelA_;
+        InterruptIn channelB_;
+        InterruptIn index_;
+    
+        int          pulsesPerRev_;
+        int          prevState_;
+        int          currState_;
+    
+        volatile int pulses_;
+
+    protected:
+    
+        Ticker      sampler;                // ticker object to sample speed
+        double      period, enc_const;      // sampling period and wheel constant
+        void sample_func(void);             // sample function
+        double getClicks(void);             // returns clickrate
+        
+    public:
+    
+        // Constructor takes 3 encoder input pins, CPR, left or right bool, sampling period, and a wheel-size constant
+        encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double ec);
+        
+        double getSpeed(void);      // returns wheel speed
+        double getDistance(void);   // returns distance travelled
+        double tempDist(void);      // returns distance travelled
+        void distRst(void);         // resets distance
+        void tempRst(void);         // resets distance
+        void start(void);           // starts recording distance
+        void reset(void);           // resets counting
+        
+        /**
+         * Read the state of the encoder.
+         *
+         * @return The current state of the encoder as a 2-bit number, where:
+         *         bit 1 = The reading from channel B
+         *         bit 2 = The reading from channel A
+         */
+        int getCurrentState(void);
+    
+        /**
+         * Read the number of pulses recorded by the encoder.
+         *
+         * @return Number of pulses which have occured.
+         */
+        int getPulses(void);
+    
+};
+
+#endif
\ No newline at end of file