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

Dependencies:   mbed Servo ros_lib_melodic ULN2003_StepperDriver Async_4pin_Stepper

Committer:
krogedal
Date:
Thu May 27 18:53:26 2021 +0000
Revision:
1:7c355adbc977
Parent:
0:441289ea4e29
Child:
3:4b6080e86761
fixed a bunch of typos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krogedal 0:441289ea4e29 1 #ifndef KARBOT_ENCODER_H
krogedal 0:441289ea4e29 2 #define KARBOT_ENCODER_H
krogedal 0:441289ea4e29 3
krogedal 0:441289ea4e29 4 /* Karbot encoder class
krogedal 0:441289ea4e29 5 *
krogedal 0:441289ea4e29 6 * This class is based upon the QEI class by Aaron Berk and the encoder class
krogedal 0:441289ea4e29 7 * I wrote during ESP in 2nd year
krogedal 0:441289ea4e29 8 *
krogedal 0:441289ea4e29 9 * Written by Simon Krogedal
krogedal 0:441289ea4e29 10 * 27/05/21
krogedal 0:441289ea4e29 11 * Team 9 4th Year project
krogedal 0:441289ea4e29 12 *
krogedal 0:441289ea4e29 13 * for NUCLEO-F401RE
krogedal 0:441289ea4e29 14 *
krogedal 0:441289ea4e29 15 */
krogedal 0:441289ea4e29 16
krogedal 0:441289ea4e29 17 #include "mbed.h"
krogedal 0:441289ea4e29 18
krogedal 0:441289ea4e29 19 #define PREV_MASK 0x1 //Mask for the previous state in determining direction
krogedal 0:441289ea4e29 20 //of rotation.
krogedal 0:441289ea4e29 21 #define CURR_MASK 0x2 //Mask for the current state in determining direction
krogedal 0:441289ea4e29 22 //of rotation.
krogedal 0:441289ea4e29 23 #define INVALID 0x3 //XORing two states where both bits have changed.
krogedal 0:441289ea4e29 24
krogedal 0:441289ea4e29 25
krogedal 0:441289ea4e29 26
krogedal 0:441289ea4e29 27 class encoder {
krogedal 0:441289ea4e29 28
krogedal 0:441289ea4e29 29 private:
krogedal 0:441289ea4e29 30 int tot_clicks, temp_tot; // clicks since last distance reset
krogedal 0:441289ea4e29 31 double click_rate, click_store;// clickrate
krogedal 0:441289ea4e29 32 bool c_d; // left or right bool
krogedal 0:441289ea4e29 33
krogedal 0:441289ea4e29 34 /**
krogedal 0:441289ea4e29 35 * Update the pulse count.
krogedal 0:441289ea4e29 36 *
krogedal 0:441289ea4e29 37 * Called on every rising/falling edge of channels A/B.
krogedal 0:441289ea4e29 38 *
krogedal 0:441289ea4e29 39 * Reads the state of the channels and determines whether a pulse forward
krogedal 0:441289ea4e29 40 * or backward has occured, updating the count appropriately.
krogedal 0:441289ea4e29 41 */
krogedal 0:441289ea4e29 42 void encode(void);
krogedal 0:441289ea4e29 43
krogedal 0:441289ea4e29 44 InterruptIn channelA_;
krogedal 0:441289ea4e29 45 InterruptIn channelB_;
krogedal 0:441289ea4e29 46
krogedal 0:441289ea4e29 47 int pulsesPerRev_;
krogedal 0:441289ea4e29 48 int prevState_;
krogedal 0:441289ea4e29 49 int currState_;
krogedal 0:441289ea4e29 50
krogedal 0:441289ea4e29 51 volatile int pulses_;
krogedal 0:441289ea4e29 52
krogedal 0:441289ea4e29 53 protected:
krogedal 0:441289ea4e29 54
krogedal 0:441289ea4e29 55 Ticker sampler; // ticker object to sample speed
krogedal 0:441289ea4e29 56 double period, enc_const; // sampling period and wheel constant
krogedal 0:441289ea4e29 57 void sample_func(void); // sample function
krogedal 0:441289ea4e29 58 double getClicks(void); // returns clickrate
krogedal 0:441289ea4e29 59
krogedal 0:441289ea4e29 60 public:
krogedal 0:441289ea4e29 61
krogedal 0:441289ea4e29 62 // Constructor takes 3 encoder input pins, CPR, left or right bool, sampling period, and a wheel-size constant
krogedal 0:441289ea4e29 63 encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double ec);
krogedal 0:441289ea4e29 64
krogedal 0:441289ea4e29 65 double getSpeed(void); // returns wheel speed
krogedal 0:441289ea4e29 66 double getDistance(void); // returns distance travelled
krogedal 0:441289ea4e29 67 double tempDist(void); // returns distance travelled
krogedal 0:441289ea4e29 68 void distRst(void); // resets distance
krogedal 0:441289ea4e29 69 void tempRst(void); // resets distance
krogedal 0:441289ea4e29 70 void start(void); // starts recording distance
krogedal 0:441289ea4e29 71 void reset(void); // resets counting
krogedal 0:441289ea4e29 72
krogedal 0:441289ea4e29 73 /**
krogedal 0:441289ea4e29 74 * Read the state of the encoder.
krogedal 0:441289ea4e29 75 *
krogedal 0:441289ea4e29 76 * @return The current state of the encoder as a 2-bit number, where:
krogedal 0:441289ea4e29 77 * bit 1 = The reading from channel B
krogedal 0:441289ea4e29 78 * bit 2 = The reading from channel A
krogedal 0:441289ea4e29 79 */
krogedal 0:441289ea4e29 80 int getCurrentState(void);
krogedal 0:441289ea4e29 81
krogedal 0:441289ea4e29 82 /**
krogedal 0:441289ea4e29 83 * Read the number of pulses recorded by the encoder.
krogedal 0:441289ea4e29 84 *
krogedal 0:441289ea4e29 85 * @return Number of pulses which have occured.
krogedal 0:441289ea4e29 86 */
krogedal 0:441289ea4e29 87 int getPulses(void);
krogedal 0:441289ea4e29 88
krogedal 0:441289ea4e29 89 };
krogedal 0:441289ea4e29 90
krogedal 0:441289ea4e29 91 #endif