P2
Revision 0:bb408887ab78, committed 2018-03-09
- Comitter:
- kueenste
- Date:
- Fri Mar 09 15:29:36 2018 +0000
- Commit message:
- P2_unfertig;
Changed in this revision
diff -r 000000000000 -r bb408887ab78 Controller.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Controller.cpp Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,175 @@ +/* + * Controller.cpp + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#include <cmath> +#include "Controller.h" + +using namespace std; + +const float Controller::PERIOD = 0.001f; // period of control task, given in [s] +const float Controller::COUNTS_PER_TURN = 1200.0f; // resolution of encoder counter +const float Controller::LOWPASS_FILTER_FREQUENCY = 300.0f; // frequency of lowpass filter for actual speed values, given in [rad/s] +const float Controller::KN = 40.0f; // speed constant of motor, given in [rpm/V] +const float Controller::KP = 0.2f; // speed controller gain, given in [V/rpm] +const float Controller::MAX_VOLTAGE = 12.0f; // supply voltage for power stage in [V] +const float Controller::MIN_DUTY_CYCLE = 0.02f; // minimum allowed value for duty cycle (2%) +const float Controller::MAX_DUTY_CYCLE = 0.98f; // maximum allowed value for duty cycle (98%) + +const float Controller::R = 0.17f/2.0f; // Abstand Antriebsraeder zu Mitte +const float Controller::r = 0.0375f; // Radius der Antriebsraeder + +/** + * Creates and initializes a Controller object. + * @param pwmLeft a pwm output object to set the duty cycle for the left motor. + * @param pwmRight a pwm output object to set the duty cycle for the right motor. + * @param counterLeft an encoder counter object to read the position of the left motor. + * @param counterRight an encoder counter object to read the position of the right motor. + */ +Controller::Controller(PwmOut& pwmLeft, PwmOut& pwmRight, EncoderCounter& counterLeft, EncoderCounter& counterRight) : pwmLeft(pwmLeft), pwmRight(pwmRight), counterLeft(counterLeft), counterRight(counterRight) { + + // initialize periphery drivers + + pwmLeft.period(0.00005f); + pwmLeft.write(0.5f); + + pwmRight.period(0.00005f); + pwmRight.write(0.5f); + + // initialize local variables + + translationalMotion.setProfileVelocity(3.0f); + translationalMotion.setProfileAcceleration(1.0f); + translationalMotion.setProfileDeceleration(1.0f); + + rotationalMotion.setProfileVelocity(2.0f); + rotationalMotion.setProfileAcceleration(2.0f); + rotationalMotion.setProfileDeceleration(2.0f); + + translationalVelocity = 0.0f; + rotationalVelocity = 0.0f; + actualTranslationalVelocity = 0.0f; + actualRotationalVelocity = 0.0f; + + previousValueCounterLeft = counterLeft.read(); + previousValueCounterRight = counterRight.read(); + + speedLeftFilter.setPeriod(PERIOD); + speedLeftFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); + + speedRightFilter.setPeriod(PERIOD); + speedRightFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); + + desiredSpeedLeft = 0.0f; + desiredSpeedRight = 0.0f; + + actualSpeedLeft = 0.0f; + actualSpeedRight = 0.0f; + + // start periodic task + + ticker.attach(callback(this, &Controller::run), PERIOD); +} + +/** + * Deletes the Controller object and releases all allocated resources. + */ +Controller::~Controller() { + + ticker.detach(); +} + +/** + * Sets the desired translational velocity of the robot. + * @param velocity the desired translational velocity, given in [m/s]. + */ +void Controller::setTranslationalVelocity(float velocity) { + + this->translationalVelocity = velocity; +} + +/** + * Sets the desired rotational velocity of the robot. + * @param velocity the desired rotational velocity, given in [rad/s]. + */ +void Controller::setRotationalVelocity(float velocity) { + + this->rotationalVelocity = velocity; +} + +/** + * Gets the actual translational velocity of the robot. + * @return the actual translational velocity, given in [m/s]. + */ +float Controller::getActualTranslationalVelocity() { + + return actualTranslationalVelocity; +} + +/** + * Gets the actual rotational velocity of the robot. + * @return the actual rotational velocity, given in [rad/s]. + */ +float Controller::getActualRotationalVelocity() { + + return actualRotationalVelocity; +} + +/** + * This method is called periodically by the ticker object and contains the + * algorithm of the speed controller. + */ +void Controller::run() { + + // calculate the planned velocities using the motion planner + + translationalMotion.incrementToVelocity(translationalVelocity,PERIOD); + rotationalMotion.incrementToVelocity(rotationalVelocity,PERIOD); + + + // calculate the values 'desiredSpeedLeft' and 'desiredSpeedRight' using the kinematic model + + desiredSpeedLeft = 60.0f*(translationalMotion.getVelocity() - R*rotationalMotion.getVelocity())/(2.0f*3.14159f*r); + // rad rechts muss invertiert werden für VORWAERTS + desiredSpeedRight = 60.0f*(-1*(translationalMotion.getVelocity() + R*rotationalMotion.getVelocity()))/(2.0f*3.14159f*r); + + // calculate actual speed of motors in [rpm] + + short valueCounterLeft = counterLeft.read(); + short valueCounterRight = counterRight.read(); + + short countsInPastPeriodLeft = valueCounterLeft-previousValueCounterLeft; + short countsInPastPeriodRight = valueCounterRight-previousValueCounterRight; + + previousValueCounterLeft = valueCounterLeft; + previousValueCounterRight = valueCounterRight; + + actualSpeedLeft = speedLeftFilter.filter((float)countsInPastPeriodLeft/COUNTS_PER_TURN/PERIOD*60.0f); + actualSpeedRight = speedRightFilter.filter((float)countsInPastPeriodRight/COUNTS_PER_TURN/PERIOD*60.0f); + + // calculate motor phase voltages + + float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN; + float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN; + + // calculate, limit and set duty cycles + + float dutyCycleLeft = 0.5f+0.5f*voltageLeft/MAX_VOLTAGE; + if (dutyCycleLeft < MIN_DUTY_CYCLE) dutyCycleLeft = MIN_DUTY_CYCLE; + else if (dutyCycleLeft > MAX_DUTY_CYCLE) dutyCycleLeft = MAX_DUTY_CYCLE; + pwmLeft.write(dutyCycleLeft); + + float dutyCycleRight = 0.5f+0.5f*voltageRight/MAX_VOLTAGE; + if (dutyCycleRight < MIN_DUTY_CYCLE) dutyCycleRight = MIN_DUTY_CYCLE; + else if (dutyCycleRight > MAX_DUTY_CYCLE) dutyCycleRight = MAX_DUTY_CYCLE; + pwmRight.write(dutyCycleRight); + + // calculate the values 'actualTranslationalVelocity' and 'actualRotationalVelocity' using the kinematic model + + //actualTranslationalVelocity = 0.5f * r *(actualSpeedLeft+actualSpeedRight); + //actualRotationalVelocity = 0.5f /R*r*(actualSpeedLeft+actualSpeedRight); + +} +
diff -r 000000000000 -r bb408887ab78 Controller.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Controller.h Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,70 @@ +/* + * Controller.h + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#ifndef CONTROLLER_H_ +#define CONTROLLER_H_ + +#include <cstdlib> +#include <mbed.h> +#include "EncoderCounter.h" +#include "Motion.h" +#include "LowpassFilter.h" + +/** + * This class implements the coordinate transformation, speed control and + * the position estimation of a mobile robot with differential drive. + */ +class Controller { + + public: + + Controller(PwmOut& pwmLeft, PwmOut& pwmRight, EncoderCounter& counterLeft, EncoderCounter& counterRight); + virtual ~Controller(); + void setTranslationalVelocity(float velocity); + void setRotationalVelocity(float velocity); + float getActualTranslationalVelocity(); + float getActualRotationalVelocity(); + + private: + + static const float PERIOD; + static const float COUNTS_PER_TURN; + static const float LOWPASS_FILTER_FREQUENCY; + static const float KN; + static const float KP; + static const float MAX_VOLTAGE; + static const float MIN_DUTY_CYCLE; + static const float MAX_DUTY_CYCLE; + + static const float R; + static const float r; + + PwmOut& pwmLeft; + PwmOut& pwmRight; + EncoderCounter& counterLeft; + EncoderCounter& counterRight; + Motion translationalMotion; + Motion rotationalMotion; + float translationalVelocity; + float rotationalVelocity; + float actualTranslationalVelocity; + float actualRotationalVelocity; + short previousValueCounterLeft; + short previousValueCounterRight; + LowpassFilter speedLeftFilter; + LowpassFilter speedRightFilter; + float desiredSpeedLeft; + float desiredSpeedRight; + float actualSpeedLeft; + float actualSpeedRight; + Ticker ticker; + + + void run(); +}; + +#endif /* CONTROLLER_H_ */ +
diff -r 000000000000 -r bb408887ab78 EncoderCounter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EncoderCounter.cpp Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,169 @@ +/* + * EncoderCounter.cpp + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#include "EncoderCounter.h" + +using namespace std; + +/** + * Creates and initializes the driver to read the quadrature + * encoder counter of the STM32 microcontroller. + * @param a the input pin for the channel A. + * @param b the input pin for the channel B. + */ +EncoderCounter::EncoderCounter(PinName a, PinName b) { + + // check pins + + if ((a == PA_0) && (b == PA_1)) { + + // pinmap OK for TIM2 CH1 and CH2 + + TIM = TIM2; + + // configure general purpose I/O registers + + GPIOA->MODER &= ~GPIO_MODER_MODER0; // reset port A0 + GPIOA->MODER |= GPIO_MODER_MODER0_1; // set alternate mode of port A0 + GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR0; // reset pull-up/pull-down on port A0 + GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1; // set input as pull-down + GPIOA->AFR[0] &= ~(0xF << 4*0); // reset alternate function of port A0 + GPIOA->AFR[0] |= 1 << 4*0; // set alternate funtion 1 of port A0 + + GPIOA->MODER &= ~GPIO_MODER_MODER1; // reset port A1 + GPIOA->MODER |= GPIO_MODER_MODER1_1; // set alternate mode of port A1 + GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR1; // reset pull-up/pull-down on port A1 + GPIOA->PUPDR |= GPIO_PUPDR_PUPDR1_1; // set input as pull-down + GPIOA->AFR[0] &= ~(0xF << 4*1); // reset alternate function of port A1 + GPIOA->AFR[0] |= 1 << 4*1; // set alternate funtion 1 of port A1 + + // configure reset and clock control registers + + RCC->APB1RSTR |= RCC_APB1RSTR_TIM2RST; //reset TIM2 controller + RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM2RST; + + RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // TIM2 clock enable + + } else if ((a == PA_6) && (b == PC_7)) { + + // pinmap OK for TIM3 CH1 and CH2 + + TIM = TIM3; + + // configure reset and clock control registers + + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // manually enable port C (port A enabled by mbed library) + + // configure general purpose I/O registers + + GPIOA->MODER &= ~GPIO_MODER_MODER6; // reset port A6 + GPIOA->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port A6 + GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port A6 + GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down + GPIOA->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port A6 + GPIOA->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port A6 + + GPIOC->MODER &= ~GPIO_MODER_MODER7; // reset port C7 + GPIOC->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port C7 + GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port C7 + GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down + GPIOC->AFR[0] &= ~0xF0000000; // reset alternate function of port C7 + GPIOC->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port C7 + + // configure reset and clock control registers + + RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST; //reset TIM3 controller + RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST; + + RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // TIM3 clock enable + + } else if ((a == PB_6) && (b == PB_7)) { + + // pinmap OK for TIM4 CH1 and CH2 + + TIM = TIM4; + + // configure reset and clock control registers + + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B (port A enabled by mbed library) + + // configure general purpose I/O registers + + GPIOB->MODER &= ~GPIO_MODER_MODER6; // reset port B6 + GPIOB->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port B6 + GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port B6 + GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down + GPIOB->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port B6 + GPIOB->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port B6 + + GPIOB->MODER &= ~GPIO_MODER_MODER7; // reset port B7 + GPIOB->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port B7 + GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port B7 + GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down + GPIOB->AFR[0] &= ~0xF0000000; // reset alternate function of port B7 + GPIOB->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port B7 + + // configure reset and clock control registers + + RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST; //reset TIM4 controller + RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST; + + RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // TIM4 clock enable + + } else { + + printf("pinmap not found for peripheral\n"); + } + + // configure general purpose timer 3 or 4 + + TIM->CR1 = 0x0000; // counter disable + TIM->CR2 = 0x0000; // reset master mode selection + TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges + TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0; + TIM->CCMR2 = 0x0000; // reset capture mode register 2 + TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E; + TIM->CNT = 0x0000; // reset counter value + TIM->ARR = 0xFFFF; // auto reload register + TIM->CR1 = TIM_CR1_CEN; // counter enable +} + +EncoderCounter::~EncoderCounter() {} + +/** + * Resets the counter value to zero. + */ +void EncoderCounter::reset() { + + TIM->CNT = 0x0000; +} + +/** + * Resets the counter value to a given offset value. + * @param offset the offset value to reset the counter to. + */ +void EncoderCounter::reset(short offset) { + + TIM->CNT = -offset; +} + +/** + * Reads the quadrature encoder counter value. + * @return the quadrature encoder counter as a signed 16-bit integer value. + */ +short EncoderCounter::read() { + + return (short)(-TIM->CNT); +} + +/** + * The empty operator is a shorthand notation of the <code>read()</code> method. + */ +EncoderCounter::operator short() { + + return read(); +} +
diff -r 000000000000 -r bb408887ab78 EncoderCounter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EncoderCounter.h Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,34 @@ +/* + * EncoderCounter.h + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#ifndef ENCODER_COUNTER_H_ +#define ENCODER_COUNTER_H_ + +#include <cstdlib> +#include <mbed.h> + +/** + * This class implements a driver to read the quadrature + * encoder counter of the STM32 microcontroller. + */ +class EncoderCounter { + + public: + + EncoderCounter(PinName a, PinName b); + virtual ~EncoderCounter(); + void reset(); + void reset(short offset); + short read(); + operator short(); + + private: + + TIM_TypeDef* TIM; +}; + +#endif /* ENCODER_COUNTER_H_ */ +
diff -r 000000000000 -r bb408887ab78 IRSensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IRSensor.cpp Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,54 @@ +/* + * IRSensor.cpp + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#include <cmath> +#include "IRSensor.h" + +using namespace std; + +/** + * Creates an IRSensor object. + * @param distance an analog input object to read the voltage of the sensor. + * @param bit0 a digital output to set the first bit of the multiplexer. + * @param bit1 a digital output to set the second bit of the multiplexer. + * @param bit2 a digital output to set the third bit of the multiplexer. + * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5. + */ +IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) { + + // set local references to objects + + this->number = number; +} + +/** + * Deletes the IRSensor object. + */ +IRSensor::~IRSensor() {} + +/** + * Gets the distance measured with the IR sensor in [m]. + * @return the distance, given in [m]. + */ +float IRSensor::read() { + + bit0 = (number >> 0) & 1; + bit1 = (number >> 1) & 1; + bit2 = (number >> 2) & 1; + + float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m] + + return d; +} + +/** + * The empty operator is a shorthand notation of the <code>read()</code> method. + */ +IRSensor::operator float() { + + return read(); +} +
diff -r 000000000000 -r bb408887ab78 IRSensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IRSensor.h Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,36 @@ +/* + * IRSensor.h + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#ifndef IR_SENSOR_H_ +#define IR_SENSOR_H_ + +#include <cstdlib> +#include <mbed.h> + +/** + * This is a device driver class to read the distance measured with a Sharp IR sensor. + */ +class IRSensor { + + public: + + IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number); + virtual ~IRSensor(); + float read(); + operator float(); + + private: + + AnalogIn& distance; + DigitalOut& bit0; + DigitalOut& bit1; + DigitalOut& bit2; + + int number; +}; + +#endif /* IR_SENSOR_H_ */ +
diff -r 000000000000 -r bb408887ab78 LowpassFilter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LowpassFilter.cpp Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,112 @@ +/* + * LowpassFilter.cpp + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#include <cmath> +#include "LowpassFilter.h" + +using namespace std; + +/** + * Creates a LowpassFilter object with a default cutoff frequency of 1000 [rad/s]. + */ +LowpassFilter::LowpassFilter() { + + period = 1.0f; + frequency = 1000.0f; + + a11 = (1.0f+frequency*period)*exp(-frequency*period); + a12 = period*exp(-frequency*period); + a21 = -frequency*frequency*period*exp(-frequency*period); + a22 = (1.0f-frequency*period)*exp(-frequency*period); + b1 = (1.0f-(1.0f+frequency*period)*exp(-frequency*period))/frequency/frequency; + b2 = period*exp(-frequency*period); + + x1 = 0.0f; + x2 = 0.0f; +} + +/** + * Deletes the LowpassFilter object. + */ +LowpassFilter::~LowpassFilter() {} + +/** + * Resets the filtered value to zero. + */ +void LowpassFilter::reset() { + + x1 = 0.0f; + x2 = 0.0f; +} + +/** + * Resets the filtered value to a given value. + * @param value the value to reset the filter to. + */ +void LowpassFilter::reset(float value) { + + x1 = value/frequency/frequency; + x2 = (x1-a11*x1-b1*value)/a12; +} + +/** + * Sets the sampling period of the filter. + * This is typically the sampling period of the periodic task of a controller that uses this filter. + * @param the sampling period, given in [s]. + */ +void LowpassFilter::setPeriod(float period) { + + this->period = period; + + a11 = (1.0f+frequency*period)*exp(-frequency*period); + a12 = period*exp(-frequency*period); + a21 = -frequency*frequency*period*exp(-frequency*period); + a22 = (1.0f-frequency*period)*exp(-frequency*period); + b1 = (1.0f-(1.0f+frequency*period)*exp(-frequency*period))/frequency/frequency; + b2 = period*exp(-frequency*period); +} + +/** + * Sets the cutoff frequency of this filter. + * @param frequency the cutoff frequency of the filter in [rad/s]. + */ +void LowpassFilter::setFrequency(float frequency) { + + this->frequency = frequency; + + a11 = (1.0f+frequency*period)*exp(-frequency*period); + a12 = period*exp(-frequency*period); + a21 = -frequency*frequency*period*exp(-frequency*period); + a22 = (1.0f-frequency*period)*exp(-frequency*period); + b1 = (1.0f-(1.0f+frequency*period)*exp(-frequency*period))/frequency/frequency; + b2 = period*exp(-frequency*period); +} + +/** + * Gets the current cutoff frequency of this filter. + * @return the current cutoff frequency in [rad/s]. + */ +float LowpassFilter::getFrequency() { + + return frequency; +} + +/** + * Filters a value. + * @param value the original unfiltered value. + * @return the filtered value. + */ +float LowpassFilter::filter(float value) { + + float x1old = x1; + float x2old = x2; + + x1 = a11*x1old+a12*x2old+b1*value; + x2 = a21*x1old+a22*x2old+b2*value; + + return frequency*frequency*x1; +} +
diff -r 000000000000 -r bb408887ab78 LowpassFilter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LowpassFilter.h Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,39 @@ +/* + * LowpassFilter.h + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#ifndef LOWPASS_FILTER_H_ +#define LOWPASS_FILTER_H_ + +#include <cstdlib> + +/** + * This class implements a time-discrete 2nd order lowpass filter for a series of data values. + * This filter can typically be used within a periodic task that takes measurements that need + * to be filtered, like speed or position values. + */ +class LowpassFilter { + + public: + + LowpassFilter(); + virtual ~LowpassFilter(); + void reset(); + void reset(float value); + void setPeriod(float period); + void setFrequency(float frequency); + float getFrequency(); + float filter(float value); + + private: + + float period; + float frequency; + float a11, a12, a21, a22, b1, b2; + float x1, x2; +}; + +#endif /* LOWPASS_FILTER_H_ */ +
diff -r 000000000000 -r bb408887ab78 Motion.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motion.cpp Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,602 @@ +/* + * Motion.cpp + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#include <cmath> +#include <algorithm> +#include "Motion.h" + +using namespace std; + +const float Motion::DEFAULT_LIMIT = 1.0f; // default value for limits +const float Motion::MINIMUM_LIMIT = 1.0e-9f; // smallest value allowed for limits + +/** + * Creates a <code>Motion</code> object. + * The values for position, velocity and acceleration are set to 0. + */ +Motion::Motion() { + + position = 0.0; + velocity = 0.0f; + + profileVelocity = DEFAULT_LIMIT; + profileAcceleration = DEFAULT_LIMIT; + profileDeceleration = DEFAULT_LIMIT; +} + +/** + * Creates a <code>Motion</code> object with given values for position and velocity. + * @param position the initial position value of this motion, given in [m] or [rad]. + * @param velocity the initial velocity value of this motion, given in [m/s] or [rad/s]. + */ +Motion::Motion(double position, float velocity) { + + this->position = position; + this->velocity = velocity; + + profileVelocity = DEFAULT_LIMIT; + profileAcceleration = DEFAULT_LIMIT; + profileDeceleration = DEFAULT_LIMIT; +} + +/** + * Creates a <code>Motion</code> object with given values for position and velocity. + * @param motion another <code>Motion</code> object to copy the values from. + */ +Motion::Motion(const Motion& motion) { + + position = motion.position; + velocity = motion.velocity; + + profileVelocity = motion.profileVelocity; + profileAcceleration = motion.profileAcceleration; + profileDeceleration = motion.profileDeceleration; +} + +/** + * Deletes the Motion object. + */ +Motion::~Motion() {} + +/** + * Sets the values for position and velocity. + * @param position the desired position value of this motion, given in [m] or [rad]. + * @param velocity the desired velocity value of this motion, given in [m/s] or [rad/s]. + */ +void Motion::set(double position, float velocity) { + + this->position = position; + this->velocity = velocity; +} + +/** + * Sets the values for position and velocity. + * @param motion another <code>Motion</code> object to copy the values from. + */ +void Motion::set(const Motion& motion) { + + position = motion.position; + velocity = motion.velocity; +} + +/** + * Sets the position value. + * @param position the desired position value of this motion, given in [m] or [rad]. + */ +void Motion::setPosition(double position) { + + this->position = position; +} + +/** + * Gets the position value. + * @return the position value of this motion, given in [m] or [rad]. + */ +double Motion::getPosition() { + + return position; +} + +/** + * Sets the velocity value. + * @param velocity the desired velocity value of this motion, given in [m/s] or [rad/s]. + */ +void Motion::setVelocity(float velocity) { + + this->velocity = velocity; +} + +/** + * Gets the velocity value. + * @return the velocity value of this motion, given in [m/s] or [rad/s]. + */ +float Motion::getVelocity() { + + return velocity; +} + +/** + * Sets the limit for the velocity value. + * @param profileVelocity the limit of the velocity. + */ +void Motion::setProfileVelocity(float profileVelocity) { + + if (profileVelocity > MINIMUM_LIMIT) this->profileVelocity = profileVelocity; else this->profileVelocity = MINIMUM_LIMIT; +} + +/** + * Sets the limit for the acceleration value. + * @param profileAcceleration the limit of the acceleration. + */ +void Motion::setProfileAcceleration(float profileAcceleration) { + + if (profileAcceleration > MINIMUM_LIMIT) this->profileAcceleration = profileAcceleration; else this->profileAcceleration = MINIMUM_LIMIT; +} + +/** + * Sets the limit for the deceleration value. + * @param profileDeceleration the limit of the deceleration. + */ +void Motion::setProfileDeceleration(float profileDeceleration) { + + if (profileDeceleration > MINIMUM_LIMIT) this->profileDeceleration = profileDeceleration; else this->profileDeceleration = MINIMUM_LIMIT; +} + +/** + * Sets the limits for velocity, acceleration and deceleration values. + * @param profileVelocity the limit of the velocity. + * @param profileAcceleration the limit of the acceleration. + * @param profileDeceleration the limit of the deceleration. + */ +void Motion::setLimits(float profileVelocity, float profileAcceleration, float profileDeceleration) { + + if (profileVelocity > MINIMUM_LIMIT) this->profileVelocity = profileVelocity; else this->profileVelocity = MINIMUM_LIMIT; + if (profileAcceleration > MINIMUM_LIMIT) this->profileAcceleration = profileAcceleration; else this->profileAcceleration = MINIMUM_LIMIT; + if (profileDeceleration > MINIMUM_LIMIT) this->profileDeceleration = profileDeceleration; else this->profileDeceleration = MINIMUM_LIMIT; +} + +/** + * Gets the time needed to move to a given target position. + * @param targetPosition the desired target position given in [m] or [rad]. + * @return the time to move to the target position, given in [s]. + */ +float Motion::getTimeToPosition(double targetPosition) { + + // calculate position, when velocity is reduced to zero + + double stopPosition = (velocity > 0.0f) ? position+(double)(velocity*velocity/profileDeceleration*0.5f) : position-(double)(velocity*velocity/profileDeceleration*0.5f); + + if (targetPosition > stopPosition) { // positive velocity required + + if (velocity > profileVelocity) { // slow down to profile velocity first + + float t1 = (velocity-profileVelocity)/profileDeceleration; + float t2 = (float)(targetPosition-stopPosition)/profileVelocity; + float t3 = profileVelocity/profileDeceleration; + + return t1+t2+t3; + + } else if (velocity > 0.0f) { // speed up to profile velocity + + float t1 = (profileVelocity-velocity)/profileAcceleration; + float t3 = profileVelocity/profileDeceleration; + float t2 = ((float)(targetPosition-position)-(velocity+profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; + + if (t2 < 0.0f) { + float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); + t1 = (maxVelocity-velocity)/profileAcceleration; + t2 = 0.0f; + t3 = maxVelocity/profileDeceleration; + } + + return t1+t2+t3; + + } else { // slow down to zero first, and then speed up to profile velocity + + float t1 = -velocity/profileDeceleration; + float t2 = profileVelocity/profileAcceleration; + float t4 = profileVelocity/profileDeceleration; + float t3 = ((float)(targetPosition-position)-velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); + + if (t3 < 0.0f) { + float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); + t2 = maxVelocity/profileAcceleration; + t3 = 0.0f; + t4 = maxVelocity/profileDeceleration; + } + + return t1+t2+t3+t4; + } + + } else { // negative velocity required + + if (velocity < -profileVelocity) { // slow down to (negative) profile velocity first + + float t1 = (-profileVelocity-velocity)/profileDeceleration; + float t2 = (float)(stopPosition-targetPosition)/profileVelocity; + float t3 = profileVelocity/profileDeceleration; + + return t1+t2+t3; + + } else if (velocity < 0.0f) { // speed up to (negative) profile velocity + + float t1 = (velocity+profileVelocity)/profileAcceleration; + float t3 = profileVelocity/profileDeceleration; + float t2 = ((float)(position-targetPosition)+(velocity-profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; + + if (t2 < 0.0f) { + float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); + t1 = (velocity-minVelocity)/profileAcceleration; + t2 = 0.0f; + t3 = -minVelocity/profileDeceleration; + } + + return t1+t2+t3; + + } else { // slow down to zero first, and then speed up to (negative) profile velocity + + float t1 = velocity/profileDeceleration; + float t2 = profileVelocity/profileAcceleration; + float t4 = profileVelocity/profileDeceleration; + float t3 = (-(float)(targetPosition-position)+velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); + + if (t3 < 0.0f) { + float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); + t2 = -minVelocity/profileAcceleration; + t3 = 0.0f; + t4 = -minVelocity/profileDeceleration; + } + + return t1+t2+t3+t4; + } + } +} + +/** + * Increments the current motion towards a given target velocity. + * @param targetVelocity the desired target velocity given in [m/s] or [rad/s]. + * @param period the time period to increment the motion values for, given in [s]. + */ +void Motion::incrementToVelocity(float targetVelocity, float period) { + + if (targetVelocity < -profileVelocity) targetVelocity = -profileVelocity; + else if (targetVelocity > profileVelocity) targetVelocity = profileVelocity; + + if (targetVelocity > 0.0f) { + + if (velocity > targetVelocity) { // slow down to target velocity + + float t1 = (velocity-targetVelocity)/profileDeceleration; + + if (t1 > period) { + position += (double)((velocity-profileDeceleration*0.5f*period)*period); + velocity += -profileDeceleration*period; + } else { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)(velocity*(period-t1)); + } + + } else if (velocity > 0.0f) { // speed up to target velocity + + float t1 = (targetVelocity-velocity)/profileAcceleration; + + if (t1 > period) { + position += (double)((velocity+profileAcceleration*0.5f*period)*period); + velocity += profileAcceleration*period; + } else { + position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); + velocity += profileAcceleration*t1; + position += (double)(velocity*(period-t1)); + } + + } else { // slow down to zero first, and then speed up to target velocity + + float t1 = -velocity/profileDeceleration; + float t2 = targetVelocity/profileAcceleration; + + if (t1 > period) { + position += (double)((velocity+profileDeceleration*0.5f*period)*period); + velocity += profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*(period-t1))*(period-t1)); + velocity += profileAcceleration*(period-t1); + } else { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); + velocity += profileAcceleration*t2; + position += (double)(velocity*(period-t1-t2)); + } + } + + } else { + + if (velocity < targetVelocity) { // slow down to (negative) target velocity + + float t1 = (targetVelocity-velocity)/profileDeceleration; + + if (t1 > period) { + position += (double)((velocity+profileDeceleration*0.5f*period)*period); + velocity += profileDeceleration*period; + } else { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)(velocity*(period-t1)); + } + + } else if (velocity < 0.0f) { // speed up to (negative) target velocity + + float t1 = (velocity-targetVelocity)/profileAcceleration; + + if (t1 > period) { + position += (double)((velocity-profileAcceleration*0.5f*period)*period); + velocity += -profileAcceleration*period; + } else { + position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); + velocity += -profileAcceleration*t1; + position += (double)(velocity*(period-t1)); + } + + } else { // slow down to zero first, and then speed up to (negative) target velocity + + float t1 = velocity/profileDeceleration; + float t2 = -targetVelocity/profileAcceleration; + + if (t1 > period) { + position += (double)((velocity-profileDeceleration*0.5f*period)*period); + velocity += -profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*(period-t1))*(period-t1)); + velocity += -profileAcceleration*(period-t1); + } else { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); + velocity += -profileAcceleration*t2; + position += (double)(velocity*(period-t1-t2)); + } + } + } +} + +/** + * Increments the current motion towards a given target position. + * @param targetPosition the desired target position given in [m] or [rad]. + * @param period the time period to increment the motion values for, given in [s]. + */ +void Motion::incrementToPosition(double targetPosition, float period) { + + // calculate position, when velocity is reduced to zero + + double stopPosition = (velocity > 0.0f) ? position+(double)(velocity*velocity/profileDeceleration*0.5f) : position-(double)(velocity*velocity/profileDeceleration*0.5f); + + if (targetPosition > stopPosition) { // positive velocity required + + if (velocity > profileVelocity) { // slow down to profile velocity first + + float t1 = (velocity-profileVelocity)/profileDeceleration; + float t2 = (float)(targetPosition-stopPosition)/profileVelocity; + float t3 = profileVelocity/profileDeceleration; + + if (t1 > period) { + position += (double)((velocity-profileDeceleration*0.5f*period)*period); + velocity += -profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)(velocity*(period-t1)); + } else if (t1+t2+t3 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); + velocity += -profileDeceleration*(period-t1-t2); + } else { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity-profileDeceleration*0.5f*t3)*t3); + velocity += -profileDeceleration*t3; + } + + } else if (velocity > 0.0f) { // speed up to profile velocity + + float t1 = (profileVelocity-velocity)/profileAcceleration; + float t3 = profileVelocity/profileDeceleration; + float t2 = ((float)(targetPosition-position)-(velocity+profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; + + if (t2 < 0.0f) { + float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); + t1 = (maxVelocity-velocity)/profileAcceleration; + t2 = 0.0f; + t3 = maxVelocity/profileDeceleration; + } + + if (t1 > period) { + position += (double)((velocity+profileAcceleration*0.5f*period)*period); + velocity += profileAcceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); + velocity += profileAcceleration*t1; + position += (double)(velocity*(period-t1)); + } else if (t1+t2+t3 > period) { + position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); + velocity += profileAcceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); + velocity += -profileDeceleration*(period-t1-t2); + } else { + position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); + velocity += profileAcceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity-profileDeceleration*0.5f*t3)*t3); + velocity += -profileDeceleration*t3; + } + + } else { // slow down to zero first, and then speed up to profile velocity + + float t1 = -velocity/profileDeceleration; + float t2 = profileVelocity/profileAcceleration; + float t4 = profileVelocity/profileDeceleration; + float t3 = ((float)(targetPosition-position)-velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); + + if (t3 < 0.0f) { + float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); + t2 = maxVelocity/profileAcceleration; + t3 = 0.0f; + t4 = maxVelocity/profileDeceleration; + } + + if (t1 > period) { + position += (double)((velocity+profileDeceleration*0.5f*period)*period); + velocity += profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*(period-t1))*(period-t1)); + velocity += profileAcceleration*(period-t1); + } else if (t1+t2+t3 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); + velocity += profileAcceleration*t2; + position += (double)(velocity*(period-t1-t2)); + } else if (t1+t2+t3+t4 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); + velocity += profileAcceleration*t2; + position += (double)(velocity*t3); + position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2-t3))*(period-t1-t2-t3)); + velocity += -profileDeceleration*(period-t1-t2-t3); + } else { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); + velocity += profileAcceleration*t2; + position += (double)(velocity*t3); + position += (double)((velocity-profileDeceleration*0.5f*t4)*t4); + velocity += -profileDeceleration*t4; + } + } + + } else { // negative velocity required + + if (velocity < -profileVelocity) { // slow down to (negative) profile velocity first + + float t1 = (-profileVelocity-velocity)/profileDeceleration; + float t2 = (float)(stopPosition-targetPosition)/profileVelocity; + float t3 = profileVelocity/profileDeceleration; + + if (t1 > period) { + position += (double)((velocity+profileDeceleration*0.5f*period)*period); + velocity += profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)(velocity*(period-t1)); + } else if (t1+t2+t3 > period) { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); + velocity += profileDeceleration*(period-t1-t2); + } else { + position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); + velocity += profileDeceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity+profileDeceleration*0.5f*t3)*t3); + velocity += profileDeceleration*t3; + } + + } else if (velocity < 0.0f) { // speed up to (negative) profile velocity + + float t1 = (velocity+profileVelocity)/profileAcceleration; + float t3 = profileVelocity/profileDeceleration; + float t2 = ((float)(position-targetPosition)+(velocity-profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; + + if (t2 < 0.0f) { + float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); + t1 = (velocity-minVelocity)/profileAcceleration; + t2 = 0.0f; + t3 = -minVelocity/profileDeceleration; + } + + if (t1 > period) { + position += (double)((velocity-profileAcceleration*0.5f*period)*period); + velocity += -profileAcceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); + velocity += -profileAcceleration*t1; + position += (double)(velocity*(period-t1)); + } else if (t1+t2+t3 > period) { + position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); + velocity += -profileAcceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); + velocity += profileDeceleration*(period-t1-t2); + } else { + position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); + velocity += -profileAcceleration*t1; + position += (double)(velocity*t2); + position += (double)((velocity+profileDeceleration*0.5f*t3)*t3); + velocity += profileDeceleration*t3; + } + + } else { // slow down to zero first, and then speed up to (negative) profile velocity + + float t1 = velocity/profileDeceleration; + float t2 = profileVelocity/profileAcceleration; + float t4 = profileVelocity/profileDeceleration; + float t3 = (-(float)(targetPosition-position)+velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); + + if (t3 < 0.0f) { + float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); + t2 = -minVelocity/profileAcceleration; + t3 = 0.0f; + t4 = -minVelocity/profileDeceleration; + } + + if (t1 > period) { + position += (double)((velocity-profileDeceleration*0.5f*period)*period); + velocity += -profileDeceleration*period; + } else if (t1+t2 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*(period-t1))*(period-t1)); + velocity += -profileAcceleration*(period-t1); + } else if (t1+t2+t3 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); + velocity += -profileAcceleration*t2; + position += (double)(velocity*(period-t1-t2)); + } else if (t1+t2+t3+t4 > period) { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); + velocity += -profileAcceleration*t2; + position += (double)(velocity*t3); + position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2-t3))*(period-t1-t2-t3)); + velocity += profileDeceleration*(period-t1-t2-t3); + } else { + position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); + velocity += -profileDeceleration*t1; + position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); + velocity += -profileAcceleration*t2; + position += (double)(velocity*t3); + position += (double)((velocity+profileDeceleration*0.5f*t4)*t4); + velocity += profileDeceleration*t4; + } + } + } +} +
diff -r 000000000000 -r bb408887ab78 Motion.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motion.h Fri Mar 09 15:29:36 2018 +0000 @@ -0,0 +1,60 @@ +/* + * Motion.h + * Copyright (c) 2018, ZHAW + * All rights reserved. + */ + +#ifndef MOTION_H_ +#define MOTION_H_ + +#include <cstdlib> + +/** + * This class keeps the motion values <code>position</code> and <code>velocity</code>, and + * offers methods to increment these values towards a desired target position or velocity. + * <br/> + * To increment the current motion values, this class uses a simple 2nd order motion planner. + * This planner calculates the motion to the target position or velocity with the various motion + * phases, based on given limits for the profile velocity, acceleration and deceleration. + * <br/> + * Note that the trajectory is calculated every time the motion state is incremented. + * This allows to change the target position or velocity, as well as the limits for profile + * velocity, acceleration and deceleration at any time. + */ +class Motion { + + public: + + double position; /**< The position value of this motion, given in [m] or [rad]. */ + float velocity; /**< The velocity value of this motion, given in [m/s] or [rad/s]. */ + + Motion(); + Motion(double position, float velocity); + Motion(const Motion& motion); + virtual ~Motion(); + void set(double position, float velocity); + void set(const Motion& motion); + void setPosition(double position); + double getPosition(); + void setVelocity(float velocity); + float getVelocity(); + void setProfileVelocity(float profileVelocity); + void setProfileAcceleration(float profileAcceleration); + void setProfileDeceleration(float profileDeceleration); + void setLimits(float profileVelocity, float profileAcceleration, float profileDeceleration); + float getTimeToPosition(double targetPosition); + void incrementToVelocity(float targetVelocity, float period); + void incrementToPosition(double targetPosition, float period); + + private: + + static const float DEFAULT_LIMIT; // default value for limits + static const float MINIMUM_LIMIT; // smallest value allowed for limits + + float profileVelocity; + float profileAcceleration; + float profileDeceleration; +}; + +#endif /* MOTION_H_ */ +