00001 /* 00002 * Defines main functions, serial commands, and data types 00003 * 00004 */ 00005 00006 #ifndef MOTOR_H 00007 #define MOTOR_H 00008 00009 #include "mbed.h" 00010 00011 #define UNASSIGNED_PWM_PIN LED4 00012 #define UNASSIGNED_MS_PIN p20 00013 #define NUM_MOTORS 7 00014 00015 ///Period of the PWM signals in microseconds 00016 #define PWM_PERIOD 64 00017 #define UPDATE_PERIOD 5000 00018 00019 class Motor 00020 { 00021 public: 00022 virtual ~Motor(); 00023 00024 /** 00025 * Update the control algorithm's parameters 00026 * 00027 * Takes in raw data from the serial port, checks the length, 00028 * and deserializes the data. 00029 * Note: it is the implementer's responsibility to deallocate 00030 * the data using the delete operator. 00031 * 00032 * @param int length 00033 * The number of bytes in the data buffer 00034 * @param uint8_t *data 00035 * The serialized parameters 00036 * @return bool 00037 * true if the data was deserialized successfully 00038 * false otherwise 00039 */ 00040 virtual bool setControlParameters(uint8_t length, uint8_t *data) = 0; 00041 00042 virtual uint8_t * readControlParameters(uint8_t & length) = 0; 00043 00044 virtual long readTargetPosition() = 0; 00045 00046 virtual long readTargetVelocity() = 0; 00047 00048 /** 00049 * Runs a single step of the control algorithm 00050 * 00051 * Updates the PWM value for the given motor 00052 */ 00053 virtual void updateMotor() = 0; 00054 00055 uint8_t readMicroswitch(); 00056 long readEncoder(); 00057 float readPWMDuty(); 00058 void setEncoder(long position); 00059 virtual void setDestination(long encoderPosition, long duration); 00060 void setPWMDuty(float duty); 00061 00062 protected: 00063 Motor(); 00064 Motor(bool internal_pwm, uint8_t external_speed_pin, 00065 PinName internal_speed_pin, PinName microswitch_pin, 00066 PinMode microswitch_mode); 00067 00068 uint8_t external_speed_pin_; 00069 bool internal_pwm_; 00070 uint8_t external_speed_; 00071 /// Hardware PWM controller 00072 PwmOut speed_; 00073 /// Microswitch input 00074 DigitalIn microswitch_; 00075 /// Reports if either parameters or positions in the process of 00076 /// being updated over serial 00077 bool update_in_progress_; 00078 /// Current encoder value 00079 long encoder_; 00080 /// Desired encoder value 00081 long desired_encoder_; 00082 /// Desired move distance 00083 long desired_distance_; 00084 /// Time since the move began 00085 Timer move_duration_; 00086 /// How long the move should take in seconds 00087 float desired_move_duration_; 00088 }; 00089 00090 #endif /* MOTOR_H */