Marco Oehler / Mbed 2 deprecated Lab2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
oehlemar
Date:
Mon Mar 09 16:23:04 2020 +0000
Commit message:
LAB2

Changed in this revision

Controller.cpp Show annotated file Show diff for this revision Revisions of this file
Controller.h Show annotated file Show diff for this revision Revisions of this file
EncoderCounter.cpp Show annotated file Show diff for this revision Revisions of this file
EncoderCounter.h Show annotated file Show diff for this revision Revisions of this file
IRSensor.cpp Show annotated file Show diff for this revision Revisions of this file
IRSensor.h Show annotated file Show diff for this revision Revisions of this file
LowpassFilter.cpp Show annotated file Show diff for this revision Revisions of this file
LowpassFilter.h Show annotated file Show diff for this revision Revisions of this file
Main.cpp Show annotated file Show diff for this revision Revisions of this file
Motion.cpp Show annotated file Show diff for this revision Revisions of this file
Motion.h Show annotated file Show diff for this revision Revisions of this file
StateMachine.cpp Show annotated file Show diff for this revision Revisions of this file
StateMachine.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Controller.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,205 @@
+/*
+ * Controller.cpp
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include "Controller.h"
+
+using namespace std;
+
+const float Controller::PERIOD = 0.001f;                    // period of 1 ms
+const float Controller::PI = 3.14159265f;                   // the constant PI
+const float Controller::WHEEL_DISTANCE = 0.185f;            // distance between wheels, given in [m]
+const float Controller::WHEEL_RADIUS = 0.038f;              // radius of wheels, given in [m]
+const float Controller::COUNTS_PER_TURN = 1200.0f;         // encoder resolution (pololu motors: 1200.0f, maxon motors: 86016.0f)
+const float Controller::LOWPASS_FILTER_FREQUENCY = 300.0f;  // given in [rad/s]
+const float Controller::KN = 40.0f;                         // speed constant in [rpm/V] (pololu motors: 40.0f, maxon motors: 45.0f)
+const float Controller::KP = 0.25f;                          // speed control parameter
+const float Controller::MAX_VOLTAGE = 12.0f;                // battery voltage in [V]
+const float Controller::MIN_DUTY_CYCLE = 0.02f;             // minimum duty-cycle
+const float Controller::MAX_DUTY_CYCLE = 0.98f;             // maximum duty-cycle
+
+/**
+ * Creates and initialises the robot controller.
+ * @param pwmLeft a reference to the pwm output for the left motor.
+ * @param pwmRight a reference to the pwm output for the right motor.
+ * @param counterLeft a reference to the encoder counter of the left motor.
+ * @param counterRight a reference to the encoder counter of the right motor.
+ */
+Controller::Controller(PwmOut& pwmLeft, PwmOut& pwmRight, EncoderCounter& counterLeft, EncoderCounter& counterRight) : pwmLeft(pwmLeft), pwmRight(pwmRight), counterLeft(counterLeft), counterRight(counterRight) {
+    
+    // initialise pwm outputs
+    
+    pwmLeft.period(0.00005f);  // pwm period of 50 us
+    pwmLeft = 0.5f;            // duty-cycle of 50%
+    
+    pwmRight.period(0.00005f); // pwm period of 50 us
+    pwmRight = 0.5f;           // duty-cycle of 50%
+    
+    // initialise local variables
+    
+    translationalMotion.setProfileVelocity(1.0f);
+    translationalMotion.setProfileAcceleration(2.0f);
+    translationalMotion.setProfileDeceleration(3.0f);
+    
+    rotationalMotion.setProfileVelocity(1.5f);
+    rotationalMotion.setProfileAcceleration(20.0f);
+    rotationalMotion.setProfileDeceleration(20.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 the periodic task
+    
+    ticker.attach(callback(this, &Controller::run), PERIOD);
+}
+
+/**
+ * Deletes this Controller object.
+ */
+Controller::~Controller() {
+    
+    ticker.detach(); // stop the periodic task
+}
+
+/**
+ * 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;
+}
+
+/**
+ * Sets the desired speed of the left motor.
+ * @param desiredSpeedLeft desired speed given in [rpm].
+ */
+void Controller::setDesiredSpeedLeft(float desiredSpeedLeft) {
+
+    this->desiredSpeedLeft = desiredSpeedLeft;
+}
+
+/**
+ * Sets the desired speed of the right motor.
+ * @param desiredSpeedRight desired speed given in [rpm].
+ */
+void Controller::setDesiredSpeedRight(float desiredSpeedRight) {
+
+    this->desiredSpeedRight = desiredSpeedRight;
+}
+
+/**
+ * Gets the actual speed of the left motor.
+ * @return the actual speed given in [rpm].
+ */
+float Controller::getActualSpeedLeft() {
+
+    return actualSpeedLeft;
+}
+
+/**
+ * Gets the actual speed of the right motor.
+ * @return the actual speed given in [rpm].
+ */
+float Controller::getActualSpeedRight() {
+
+    return actualSpeedRight;
+}
+
+/**
+ * This is an internal method of the controller that is running periodically.
+ */
+void Controller::run() {
+ 
+    // calculate the planned velocities using the motion planner
+    translationalMotion.incrementToVelocity(translationalVelocity, PERIOD); // Schnelligkeit 0.5 m/s, Periode 0.001 Sek.
+    rotationalMotion.incrementToVelocity(rotationalVelocity, PERIOD);
+    
+    // calculate the values 'desiredSpeedLeft' and 'desiredSpeedRight' using the kinematic model
+    desiredSpeedLeft = translationalVelocity - (WHEEL_DISTANCE/2)*rotationalVelocity;
+    desiredSpeedRight = translationalVelocity + (WHEEL_DISTANCE/2)*rotationalVelocity;
+    
+    // calculate the actual speed of the motors in [rpm]
+    desiredSpeedLeft = (60/(2*PI*WHEEL_RADIUS))*desiredSpeedLeft;
+    desiredSpeedRight = (-60/(2*PI*WHEEL_RADIUS))*desiredSpeedRight;
+    
+    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 desired motor voltages Uout
+
+    float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN;
+    float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN;
+
+    // calculate, limit and set the duty-cycle
+
+    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 = 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 = dutyCycleRight;
+    
+    // calculate the values 'actualTranslationalVelocity' and 'actualRotationalVelocity' using the kinematic model
+    actualTranslationalVelocity = 0.5*(actualSpeedLeft+actualSpeedRight);
+    actualRotationalVelocity = 1/(WHEEL_DISTANCE)*(actualSpeedRight-actualSpeedLeft);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Controller.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,73 @@
+/*
+ * Controller.h
+ * Copyright (c) 2020, 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 a controller that regulates the
+ * speed of the two motors of the ROME2 mobile robot.
+ */
+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();
+        void    setDesiredSpeedLeft(float desiredSpeedLeft);
+        void    setDesiredSpeedRight(float desiredSpeedRight);
+        float   getActualSpeedLeft();
+        float   getActualSpeedRight();
+        
+  private:
+        
+        static const float  PERIOD;
+        static const float  PI;
+        static const float  WHEEL_DISTANCE;
+        static const float  WHEEL_RADIUS;
+        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;
+
+        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_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EncoderCounter.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,182 @@
+/*
+ * EncoderCounter.cpp
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include "EncoderCounter.h"
+
+using namespace std;
+
+/**
+ * Creates and initialises 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_15) && (b == PB_3)) {
+        
+        // pinmap OK for TIM2 CH1 and CH2
+        
+        TIM = TIM2;
+        
+        // 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
+        
+        GPIOA->MODER &= ~GPIO_MODER_MODER15;    // reset port A15
+        GPIOA->MODER |= GPIO_MODER_MODER15_1;   // set alternate mode of port A15
+        GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR15;    // reset pull-up/pull-down on port A15
+        GPIOA->PUPDR |= GPIO_PUPDR_PUPDR15_1;   // set input as pull-down
+        GPIOA->AFR[1] &= ~0xF0000000;           // reset alternate function of port A15
+        GPIOA->AFR[1] |= 1 << 4*7;              // set alternate funtion 1 of port A15
+        
+        GPIOB->MODER &= ~GPIO_MODER_MODER3;     // reset port B3
+        GPIOB->MODER |= GPIO_MODER_MODER3_1;    // set alternate mode of port B3
+        GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR3;     // reset pull-up/pull-down on port B3
+        GPIOB->PUPDR |= GPIO_PUPDR_PUPDR3_1;    // set input as pull-down
+        GPIOB->AFR[0] &= ~(0xF << 4*3);         // reset alternate function of port B3
+        GPIOB->AFR[0] |= 1 << 4*3;              // set alternate funtion 1 of port B3
+        
+        // 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 == PB_4) && (b == PC_7)) {
+        
+        // pinmap OK for TIM3 CH1 and CH2
+        
+        TIM = TIM3;
+        
+        // configure reset and clock control registers
+        
+        RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;    // manually enable port B
+        RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;    // manually enable port C
+        
+        // configure general purpose I/O registers
+        
+        GPIOB->MODER &= ~GPIO_MODER_MODER4;     // reset port B4
+        GPIOB->MODER |= GPIO_MODER_MODER4_1;    // set alternate mode of port B4
+        GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR4;     // reset pull-up/pull-down on port B4
+        GPIOB->PUPDR |= GPIO_PUPDR_PUPDR4_1;    // set input as pull-down
+        GPIOB->AFR[0] &= ~(0xF << 4*4);         // reset alternate function of port B4
+        GPIOB->AFR[0] |= 2 << 4*4;              // set alternate funtion 2 of port B4
+        
+        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 == PD_12) && (b == PD_13)) {
+        
+        // pinmap OK for TIM4 CH1 and CH2
+        
+        TIM = TIM4;
+        
+        // configure reset and clock control registers
+        
+        RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;    // manually enable port D
+        
+        // configure general purpose I/O registers
+        
+        GPIOD->MODER &= ~GPIO_MODER_MODER12;    // reset port D12
+        GPIOD->MODER |= GPIO_MODER_MODER12_1;   // set alternate mode of port D12
+        GPIOD->PUPDR &= ~GPIO_PUPDR_PUPDR12;    // reset pull-up/pull-down on port D12
+        GPIOD->PUPDR |= GPIO_PUPDR_PUPDR12_1;   // set input as pull-down
+        GPIOD->AFR[1] &= ~(0xF << 4*4);         // reset alternate function of port D12
+        GPIOD->AFR[1] |= 2 << 4*4;              // set alternate funtion 2 of port D12
+        
+        GPIOD->MODER &= ~GPIO_MODER_MODER13;    // reset port D13
+        GPIOD->MODER |= GPIO_MODER_MODER13_1;   // set alternate mode of port D13
+        GPIOD->PUPDR &= ~GPIO_PUPDR_PUPDR13;    // reset pull-up/pull-down on port D13
+        GPIOD->PUPDR |= GPIO_PUPDR_PUPDR13_1;   // set input as pull-down
+        GPIOD->AFR[1] &= ~(0xF << 4*5);         // reset alternate function of port D13
+        GPIOD->AFR[1] |= 2 << 4*5;              // set alternate funtion 2 of port D13
+        
+        // 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");
+        
+        TIM = NULL;
+    }
+    
+    // configure general purpose timer 2, 3 or 4
+    
+    if (TIM != NULL) {
+        
+        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
+    }
+}
+
+/**
+ * Deletes this EncoderCounter object.
+ */
+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();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EncoderCounter.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,34 @@
+/*
+ * EncoderCounter.h
+ * Copyright (c) 2020, 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_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRSensor.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,52 @@
+/*
+ * IRSensor.h
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include <cmath>
+#include "IRSensor.h"
+
+using namespace std;
+
+/**
+ * Creates and initialises the driver to read the distance sensors.
+ * @param distance the analog input to read a distance value from.
+ * @param bit0 a digital output to control the multiplexer.
+ * @param bit1 a digital output to control the multiplexer.
+ * @param bit2 a digital output to control the multiplexer.
+ * @param number the number of the sensor. This value must be between 0 and 5.
+ */
+IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
+    
+    this->number = number;
+}
+
+/**
+ * Deletes this IRSensor object and releases all allocated resources.
+ */
+IRSensor::~IRSensor() {}
+
+/**
+ * This method reads from the distance sensor.
+ * @return a distance value, 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();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRSensor.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,36 @@
+/*
+ * IRSensor.h
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#ifndef IR_SENSOR_H_
+#define IR_SENSOR_H_
+
+#include <cstdlib>
+#include <mbed.h>
+
+/**
+ * This class implements a driver to read the distance sensors
+ * of the ROME2 mobile robot.
+ */
+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_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LowpassFilter.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,112 @@
+/*
+ * LowpassFilter.cpp
+ * Copyright (c) 2020, 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 this 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;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LowpassFilter.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,39 @@
+/*
+ * LowpassFilter.h
+ * Copyright (c) 2020, 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_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Main.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,92 @@
+/*
+ * Main.cpp
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include <mbed.h>
+#include "IRSensor.h"
+#include "EncoderCounter.h"
+#include "Controller.h"
+#include "StateMachine.h"
+
+int main() {
+    
+    // initialise digital inputs and outputs
+    
+    DigitalIn button(USER_BUTTON);
+    
+    DigitalOut ledGreen(LED1);
+    DigitalOut ledBlue(LED2);
+    DigitalOut ledRed(LED3);
+    
+    DigitalOut led0(PD_4);
+    DigitalOut led1(PD_3);
+    DigitalOut led2(PD_6);
+    DigitalOut led3(PD_2);
+    DigitalOut led4(PD_7);
+    DigitalOut led5(PD_5);
+    
+    // create distance sensor objects
+    
+    AnalogIn distance(PA_0);
+    DigitalOut enable(PG_1);
+    DigitalOut bit0(PF_0);
+    DigitalOut bit1(PF_1);
+    DigitalOut bit2(PF_2);
+    
+    enable = 1;
+    
+    IRSensor irSensor0(distance, bit0, bit1, bit2, 0);
+    IRSensor irSensor1(distance, bit0, bit1, bit2, 1);
+    IRSensor irSensor2(distance, bit0, bit1, bit2, 2);
+    IRSensor irSensor3(distance, bit0, bit1, bit2, 3);
+    IRSensor irSensor4(distance, bit0, bit1, bit2, 4);
+    IRSensor irSensor5(distance, bit0, bit1, bit2, 5);
+    
+    // create motor controller objects
+    
+    DigitalOut enableMotorDriver(PG_0); 
+    DigitalIn motorDriverFault(PD_1);
+    DigitalIn motorDriverWarning(PD_0);
+    
+    PwmOut pwmLeft(PF_9);
+    PwmOut pwmRight(PF_8);
+    
+    // create encoder counter objects
+    
+    EncoderCounter counterLeft(PD_12, PD_13);
+    EncoderCounter counterRight(PB_4, PC_7);
+    
+    // create robot controller objects
+    
+    Controller controller(pwmLeft, pwmRight, counterLeft, counterRight);
+    StateMachine stateMachine(controller, enableMotorDriver, led0, led1, led2, led3, led4, led5, button, irSensor0, irSensor1, irSensor2, irSensor3, irSensor4, irSensor5);
+    
+    // enter main loop
+    
+    while (true) {
+        
+        if (stateMachine.getState() == StateMachine::MOVE_FORWARD) {
+            
+            ledGreen = 1;
+            ledBlue = 0;
+            ledRed = 0;
+            
+        } else if ((stateMachine.getState() == StateMachine::TURN_LEFT) || (stateMachine.getState() == StateMachine::TURN_RIGHT)) {
+            
+            ledGreen = 1;
+            ledBlue = 1;
+            ledRed = 0;
+            
+        } else {
+            
+            ledGreen = 0;
+            ledBlue = 0;
+            ledRed = 1;
+        }
+        
+        wait(0.1);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motion.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,602 @@
+/*
+ * Motion.cpp
+ * Copyright (c) 2020, 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;
+            }
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motion.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,60 @@
+/*
+ * Motion.h
+ * Copyright (c) 2020, 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_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,148 @@
+/*
+ * StateMachine.cpp
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include <cmath>
+#include "StateMachine.h"
+
+using namespace std;
+
+const float StateMachine::PERIOD = 0.01f;                   // period of task in [s]
+const float StateMachine::DISTANCE_THRESHOLD = 0.25f;        // minimum allowed distance to obstacle in [m]
+const float StateMachine::TRANSLATIONAL_VELOCITY = 1.0f;    // translational velocity in [m/s]
+const float StateMachine::ROTATIONAL_VELOCITY = 1.5f;       // rotational velocity in [rad/s]
+
+/**
+ * Creates and initializes a state machine object.
+ */
+StateMachine::StateMachine(Controller& controller, DigitalOut& enableMotorDriver, DigitalOut& led0, DigitalOut& led1, DigitalOut& led2, DigitalOut& led3, DigitalOut& led4, DigitalOut& led5, DigitalIn& button, IRSensor& irSensor0, IRSensor& irSensor1, IRSensor& irSensor2, IRSensor& irSensor3, IRSensor& irSensor4, IRSensor& irSensor5) : controller(controller), enableMotorDriver(enableMotorDriver), led0(led0), led1(led1), led2(led2), led3(led3), led4(led4), led5(led5), button(button), irSensor0(irSensor0), irSensor1(irSensor1), irSensor2(irSensor2), irSensor3(irSensor3), irSensor4(irSensor4), irSensor5(irSensor5) {
+    
+    enableMotorDriver = 0;
+    state = ROBOT_OFF;
+    buttonNow = button;
+    buttonBefore = buttonNow;
+    
+    ticker.attach(callback(this, &StateMachine::run), PERIOD);
+}
+
+/**
+ * Deletes the state machine object and releases all allocated resources.
+ */
+StateMachine::~StateMachine() {
+    
+    ticker.detach();
+}
+
+/**
+ * Gets the actual state of this state machine.
+ * @return the actual state as an int constant.
+ */
+int StateMachine::getState() {
+    
+    return state;
+}
+
+/**
+ * This method is called periodically by the ticker object and implements the
+ * logic of the state machine.
+ */
+void StateMachine::run() {
+    
+    // set the leds based on distance measurements
+    
+    led0 = irSensor0 < DISTANCE_THRESHOLD;
+    led1 = irSensor1 < DISTANCE_THRESHOLD;
+    led2 = irSensor2 < DISTANCE_THRESHOLD;
+    led3 = irSensor3 < DISTANCE_THRESHOLD;
+    led4 = irSensor4 < DISTANCE_THRESHOLD;
+    led5 = irSensor5 < DISTANCE_THRESHOLD;
+    
+    // implementation of the state machine
+    
+    switch (state) {
+        
+        case ROBOT_OFF:
+            
+            buttonNow = button;
+            
+            if (buttonNow && !buttonBefore) {   // detect button rising edge
+                
+                enableMotorDriver = 1;
+                
+                controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY);
+                controller.setRotationalVelocity(0.0f);
+                
+                state = MOVE_FORWARD;
+            }
+            
+            buttonBefore = buttonNow;
+            
+            break;
+            
+        case MOVE_FORWARD:
+            
+            if (!led2 && !led3 && !led4) {
+                controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY);
+                controller.setRotationalVelocity(0.0f);
+                }
+            else if(led2 || (led2&&led3)){
+                state = TURN_LEFT;
+                }
+            else if(led4 || (led3&&led4)){
+                state = TURN_RIGHT;
+                }
+            else if(led2 && led3 && led4){
+               state = SLOWING_DOWN;
+               }
+                
+                
+            // bitte implementieren!
+            
+            break;
+            
+        case TURN_LEFT:
+       
+            
+           
+            if(led4 || led3 || (led4&&led3)){
+                controller.setTranslationalVelocity(0.0f);
+                controller.setRotationalVelocity(-ROTATIONAL_VELOCITY);
+                }
+         
+            state = MOVE_FORWARD;
+            // bitte implementieren!
+            
+            break;
+            
+        case TURN_RIGHT:
+        
+            if(led3 || led2 || (led2&&led3)){
+                controller.setTranslationalVelocity(0.0f);
+                controller.setRotationalVelocity(ROTATIONAL_VELOCITY);
+                }
+            
+            state = MOVE_FORWARD;
+            // bitte implementieren!
+            
+            break;
+            
+        case SLOWING_DOWN:
+            if (controller.getActualTranslationalVelocity()>0.0f){
+                    controller.setTranslationalVelocity(0.0f);
+            } else if (controller.getActualTranslationalVelocity()<= 0.0001f){
+                enableMotorDriver = 0;
+                }
+            state = ROBOT_OFF;
+            
+            // bitte implementieren!
+            
+            break;
+            
+        default:
+            
+            state = ROBOT_OFF;
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.h	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,66 @@
+/*
+ * StateMachine.h
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#ifndef STATE_MACHINE_H_
+#define STATE_MACHINE_H_
+
+#include <cstdlib>
+#include <mbed.h>
+#include "Controller.h"
+#include "IRSensor.h"
+
+/**
+ * This class implements a simple state machine for a mobile robot.
+ * It allows to move the robot forward, and to turn left or right,
+ * depending on distance measurements, to avoid collisions with
+ * obstacles.
+ */
+class StateMachine {
+    
+    public:
+        
+        static const int    ROBOT_OFF = 0;      // discrete states of this state machine
+        static const int    MOVE_FORWARD = 1;
+        static const int    TURN_LEFT = 2;
+        static const int    TURN_RIGHT = 3;
+        static const int    SLOWING_DOWN = 4;
+        
+                    StateMachine(Controller& controller, DigitalOut& enableMotorDriver, DigitalOut& led0, DigitalOut& led1, DigitalOut& led2, DigitalOut& led3, DigitalOut& led4, DigitalOut& led5, DigitalIn& button, IRSensor& irSensor0, IRSensor& irSensor1, IRSensor& irSensor2, IRSensor& irSensor3, IRSensor& irSensor4, IRSensor& irSensor5);
+        virtual     ~StateMachine();
+        int         getState();
+        
+    private:
+        
+        static const float  PERIOD;                 // period of task in [s]
+        static const float  DISTANCE_THRESHOLD;     // minimum allowed distance to obstacle in [m]
+        static const float  TRANSLATIONAL_VELOCITY; // translational velocity in [m/s]
+        static const float  ROTATIONAL_VELOCITY;    // rotational velocity in [rad/s]
+        
+        Controller&     controller;
+        DigitalOut&     enableMotorDriver;
+        DigitalOut&     led0;
+        DigitalOut&     led1;
+        DigitalOut&     led2;
+        DigitalOut&     led3;
+        DigitalOut&     led4;
+        DigitalOut&     led5;
+        DigitalIn&      button;
+        IRSensor&       irSensor0;
+        IRSensor&       irSensor1;
+        IRSensor&       irSensor2;
+        IRSensor&       irSensor3;
+        IRSensor&       irSensor4;
+        IRSensor&       irSensor5;
+        int             state;
+        int             buttonNow;
+        int             buttonBefore;
+        Ticker          ticker;
+        
+        void            run();
+};
+
+#endif /* STATE_MACHINE_H_ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file