robot code for summer school
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
IRSensor.cpp@49:7da71f479dac, 2022-05-25 (annotated)
- Committer:
- seas726
- Date:
- Wed May 25 11:36:19 2022 +0200
- Revision:
- 49:7da71f479dac
speed control new files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seas726 | 49:7da71f479dac | 1 | /* |
seas726 | 49:7da71f479dac | 2 | * IRSensor.h |
seas726 | 49:7da71f479dac | 3 | * Copyright (c) 2022, ZHAW |
seas726 | 49:7da71f479dac | 4 | * All rights reserved. |
seas726 | 49:7da71f479dac | 5 | */ |
seas726 | 49:7da71f479dac | 6 | |
seas726 | 49:7da71f479dac | 7 | #include <cmath> |
seas726 | 49:7da71f479dac | 8 | #include "IRSensor.h" |
seas726 | 49:7da71f479dac | 9 | |
seas726 | 49:7da71f479dac | 10 | using namespace std; |
seas726 | 49:7da71f479dac | 11 | |
seas726 | 49:7da71f479dac | 12 | /** |
seas726 | 49:7da71f479dac | 13 | * Creates and initialises the driver to read the distance sensors. |
seas726 | 49:7da71f479dac | 14 | * @param distance the analog input to read a distance value from. |
seas726 | 49:7da71f479dac | 15 | * @param bit0 a digital output to control the multiplexer. |
seas726 | 49:7da71f479dac | 16 | * @param bit1 a digital output to control the multiplexer. |
seas726 | 49:7da71f479dac | 17 | * @param bit2 a digital output to control the multiplexer. |
seas726 | 49:7da71f479dac | 18 | * @param number the number of the sensor. This value must be between 0 and 5. |
seas726 | 49:7da71f479dac | 19 | */ |
seas726 | 49:7da71f479dac | 20 | IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) { |
seas726 | 49:7da71f479dac | 21 | |
seas726 | 49:7da71f479dac | 22 | this->number = number; |
seas726 | 49:7da71f479dac | 23 | } |
seas726 | 49:7da71f479dac | 24 | |
seas726 | 49:7da71f479dac | 25 | /** |
seas726 | 49:7da71f479dac | 26 | * Deletes this IRSensor object and releases all allocated resources. |
seas726 | 49:7da71f479dac | 27 | */ |
seas726 | 49:7da71f479dac | 28 | IRSensor::~IRSensor() {} |
seas726 | 49:7da71f479dac | 29 | |
seas726 | 49:7da71f479dac | 30 | /** |
seas726 | 49:7da71f479dac | 31 | * This method reads from the distance sensor. |
seas726 | 49:7da71f479dac | 32 | * @return a distance value, given in [m]. |
seas726 | 49:7da71f479dac | 33 | */ |
seas726 | 49:7da71f479dac | 34 | float IRSensor::read() { |
seas726 | 49:7da71f479dac | 35 | |
seas726 | 49:7da71f479dac | 36 | bit0 = (number >> 0) & 1; |
seas726 | 49:7da71f479dac | 37 | bit1 = (number >> 1) & 1; |
seas726 | 49:7da71f479dac | 38 | bit2 = (number >> 2) & 1; |
seas726 | 49:7da71f479dac | 39 | |
seas726 | 49:7da71f479dac | 40 | float d = 0.09f/(distance+0.001f)-0.03f; // calculate the distance in [m] |
seas726 | 49:7da71f479dac | 41 | |
seas726 | 49:7da71f479dac | 42 | return d; |
seas726 | 49:7da71f479dac | 43 | } |
seas726 | 49:7da71f479dac | 44 | |
seas726 | 49:7da71f479dac | 45 | /** |
seas726 | 49:7da71f479dac | 46 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
seas726 | 49:7da71f479dac | 47 | */ |
seas726 | 49:7da71f479dac | 48 | /* |
seas726 | 49:7da71f479dac | 49 | IRSensor::operator float() { |
seas726 | 49:7da71f479dac | 50 | |
seas726 | 49:7da71f479dac | 51 | return read(); |
seas726 | 49:7da71f479dac | 52 | } |
seas726 | 49:7da71f479dac | 53 | */ |