Controls a 12V linear actuator up and down as used in the https://os.mbed.com/users/cnckiwi31/code/heros_leg_readout_torque_addition/ test rig

Dependents:   heros_leg_readout_torque_addition_V3 DROPSAWTestRigCode_V4

LinearActuator.h

Committer:
cnckiwi31
Date:
2020-08-07
Revision:
0:5cc1284be040
Child:
1:35bf896ac56e

File content as of revision 0:5cc1284be040:

#ifndef _LINACT_H_
#define _LINACT_H_

#include "mbed.h"
/**
 * Controlling aa linear actuator for moving the test rig joint up and down
 */

class LinearActuator
{
public:

    /**
     * @param dir_pin PinName of digital output controlling actuator direction (1 is retract, 0 is extend)
     * @param pwm_pin PinName of digital pwm output controlling actuator speed
     */
    LinearActuator (PinName dir_pin, PinName pwm_pin) :
        DirPin(dir_pin),PwmPin(pwm_pin)
    {
        PwmPin.period(1/timing::PWMHertz);  // 10kHz PWM
        
        //default to off
        setDirection(false);
        setPWM(0);
    }

    /**
     * @return current direction
     */
    bool getDirection()
    {
        return DirPin.read();
    }

    /**
     * @param dir set current direction (true is retract, false is extend)
     */
    void setDirection(bool dir)
    {
        DirPin.write((int)dir);
        return;
    }

    /**
     * @param DutyCycle is percent of pwm
     */
    void setPWM(int DutyCycle)
    {
        //limit the duty cycle to 0-100%
        if(DutyCycle > 100) {
            DutyCycle = 100;
        } else if (DutyCycle < 0) {
            DutyCycle = 0;
        }
        //convert to a pulse width in seconds
        double onTime = (1/(double)timing::PWMHertz)*((double)DutyCycle/100);
        PwmPin.pulsewidth(onTime);
        return;
    }

    
private:
    DigitalOut DirPin;
    PwmOut PwmPin;
};

#endif