Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 36870ed

Browse files
committed
Use 4 VL53L1 distance sensors in combination with MCP23017.
2 parents ef60ed0 + 02f5237 commit 36870ed

File tree

9 files changed

+399
-81
lines changed

9 files changed

+399
-81
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ Also the IO-expander `I11` has been disconnected in order to be able to control
6161
Software
6262
========
6363

64-
The source code is written in C++. It is compiled and programmed with the [Arduino IDE](https://www.arduino.cc/en/Main/Software). The [board support package for the ESP8266 chip](https://github.com/esp8266/Arduino) must be installed.
64+
The source code is written in C++. It is compiled and programmed with the [Arduino IDE](https://www.arduino.cc/en/Main/Software). The [board support package for the ESP8266 chip](https://github.com/esp8266/Arduino) must be installed.
65+
The [Arduino library VL53L1](https://github.com/stm32duino/VL53L1) must be installed.

robot-control-src/DigitalPin.hpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

robot-control-src/GpioInterface.hpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,70 @@
33

44
#include <cstdint>
55

6-
class GpioInterface {
6+
/**
7+
* Interface for digital input and output pins.
8+
*
9+
* Implementations may provide access to the pins of the microprocessor or
10+
* external pins of IO-expanders.
11+
*
12+
* Parameters and return values are compatible to the Arduino library.
13+
*/
14+
class GpioInterface
15+
{
716
public:
17+
/**
18+
* Sets output value of the pin.
19+
* @param value may be HIGH or LOW (see Arduino.h)
20+
* @pre Configure the mode to OUTPUT using setMode().
21+
*/
822
virtual void write(const std::uint8_t value) = 0;
23+
24+
/**
25+
* Reads the value of the pin.
26+
* @return Either HIGH or LOW (see Arduino.h).
27+
* @pre Configure the mode to INPUT or INPUT_PULLUP using setMode().
28+
*/
929
virtual int read() = 0;
30+
31+
/**
32+
* Configures the pin to be either input or output.
33+
* @param mode INPUT, OUTPUT, or INPUT_PULLUP (see Arduino.h).
34+
*/
1035
virtual void setMode(const std::uint8_t mode) = 0;
1136

12-
virtual ~GpioInterface() {};
37+
virtual ~GpioInterface() = default;
1338
};
1439

40+
/** @name Digital I/O function overload for GpioInterface objects.
41+
*
42+
* These functions provide an interface to GpioInterface objects compatible
43+
* to the Arduino library digital I/O functions.
44+
*/
45+
/** @{ */ /* Start group for digital I/O */
46+
47+
/**
48+
* Sets output value of the pin.
49+
* @param gpio digital output pin
50+
* @param value may be HIGH or LOW (see Arduino.h)
51+
* @pre Configure the mode to OUTPUT using pinMode().
52+
*/
1553
void digitalWrite(GpioInterface& gpio, const std::uint8_t value);
54+
55+
/**
56+
* Reads the value of the pin.
57+
* @param gpio digital input pin
58+
* @return Either HIGH or LOW (see Arduino.h).
59+
* @pre Configure the mode to INPUT or INPUT_PULLUP using pinMode().
60+
*/
1661
int digitalRead(GpioInterface& gpio);
62+
63+
/**
64+
* Configures the pin to be either input or output.
65+
* @param gpio digital pin
66+
* @param mode INPUT, OUTPUT, or INPUT_PULLUP (see Arduino.h).
67+
*/
1768
void pinMode(GpioInterface& gpio, const std::uint8_t mode);
1869

70+
/** @} */ /* End group for digital I/O */
71+
1972
#endif /* GPIOINTERFACE_HPP_ */

robot-control-src/GpioPin.hpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#ifndef GPIOPIN_HPP_
2+
#define GPIOPIN_HPP_
3+
4+
#include "GpioInterface.hpp"
5+
#include <Arduino.h>
6+
7+
/**
8+
* Extension of GpioInterface for the processors general purpose input/output pins.
9+
*
10+
* Adds method to set PWM duty cycle.
11+
*
12+
* @tparam PIN is the Arduino pin number
13+
*/
14+
template<std::uint8_t PIN>
15+
class GpioPin: public GpioInterface
16+
{
17+
public:
18+
static constexpr std::uint8_t pin = PIN;
19+
20+
virtual int read() override
21+
{
22+
return digitalRead(pin);
23+
}
24+
25+
virtual void setMode(const std::uint8_t mode) override
26+
{
27+
pinMode(pin, mode);
28+
}
29+
30+
virtual void write(const std::uint8_t value) override
31+
{
32+
digitalWrite(pin, value);
33+
}
34+
35+
/**
36+
* Sets PWM duty cycle.
37+
* @param dutyCycle between 0 and 255
38+
*/
39+
void setDutyCycle(const int dutyCycle) const
40+
{
41+
analogWrite(pin, dutyCycle);
42+
}
43+
};
44+
45+
/* If digitalPinToInterrupt is defined as C preprocessor macro it needs to be
46+
* undefined. digitalPinToInterrupt must be overloaded for GpioPin. This is
47+
* not possible for macros.
48+
*/
49+
#if defined(digitalPinToInterrupt)
50+
51+
/**
52+
* Wrapper for digitalPinToInterrupt.
53+
*
54+
* Encapsulates the digitalPinToInterrupt functionality before it is undefined.
55+
* In contrast to the C preprocessor macro this function can be overridden.
56+
*
57+
* @param pinNumber is the Arduino pin number
58+
* @return interrupt number
59+
*/
60+
inline constexpr int digitalPinToInterruptWrapper(const int pinNumber)
61+
{
62+
return digitalPinToInterrupt(pinNumber);
63+
}
64+
65+
/* Undefine macro in order to enable function declaration with the same name. */
66+
#undef digitalPinToInterrupt
67+
68+
/**
69+
* Getter for the interrupt number for a digital input pin.
70+
*
71+
* This is an adapter to digitalPinToInterruptWrapper().
72+
* @param pinNumber is the Arduino pin number
73+
* @return interrupt number
74+
*/
75+
inline constexpr int digitalPinToInterrupt(const int pinNumber)
76+
{
77+
return digitalPinToInterruptWrapper(pinNumber);
78+
}
79+
80+
#endif /* defined(digitalPinToInterrupt) */
81+
82+
/** @name Function overload for GpioPin objects.
83+
*
84+
* These functions provide an interface to GpioPin objects compatible
85+
* to the Arduino library functions.
86+
*/
87+
/** @{ */ /* Start group for function overloads */
88+
89+
/**
90+
* Getter for the interrupt number for a digital input pin.
91+
*
92+
* This is an overload to digitalPinToInterrupt().
93+
* @tparam PIN is the Arduino pin number
94+
* @return interrupt number
95+
*/
96+
template<std::uint8_t PIN>
97+
int constexpr digitalPinToInterrupt(const GpioPin<PIN>& gpio)
98+
{
99+
return digitalPinToInterrupt(gpio.pin);
100+
}
101+
102+
/**
103+
* Attach function to be called on external interrupt.
104+
*
105+
* @param gpio is the input pin to attach interrupt to
106+
* @param interruptServiceRoutine is the function to be called when interrupt occurs
107+
* @param mode may be LOW, CHANGE, RISING, FALLING (see Arduino.h)
108+
* @tparam PIN is the Arduino pin number
109+
*/
110+
template<std::uint8_t PIN>
111+
void attachInterrupt(const GpioPin<PIN>& gpio, void (* const interruptServiceRoutine)(void), const int mode)
112+
{
113+
attachInterrupt(digitalPinToInterrupt(gpio.pin), interruptServiceRoutine, mode);
114+
}
115+
116+
/**
117+
* Sets PWM duty cycle to pin.
118+
* @tparam PIN is the Arduino pin number
119+
* @param gpio output pin to set duty cycle to.
120+
* @param dutyCycle between 0 and 255
121+
*/
122+
template<std::uint8_t PIN>
123+
void analogWrite(const GpioPin<PIN>& gpio, const int dutyCycle)
124+
{
125+
analogWrite(gpio.pin, dutyCycle);
126+
}
127+
128+
/** @} */ /* End group for function overloads */
129+
130+
#endif /* GPIOPIN_HPP_ */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "VL53L1GpioInterface.hpp"
2+
#include "GpioInterface.hpp"
3+
#include <Arduino.h>
4+
5+
VL53L1GpioInterface::~VL53L1GpioInterface() {
6+
end();
7+
}
8+
9+
VL53L1GpioInterface::VL53L1GpioInterface(TwoWire *const i2cDevice,
10+
GpioInterface &xshutdownPin) : VL53L1(i2cDevice, -1), xshutdown(xshutdownPin) {
11+
}
12+
13+
void VL53L1GpioInterface::VL53L1_SetDeviceAddressValue(
14+
std::uint8_t const newValue) {
15+
VL53L1_SetDeviceAddress(newValue << 1);
16+
}
17+
18+
std::uint8_t VL53L1GpioInterface::VL53L1_GetDeviceAddressValue() const {
19+
return MyDevice.I2cDevAddr >> 1;
20+
}
21+
22+
VL53L1_Error VL53L1GpioInterface::initSensorWithAddressValue(
23+
std::uint8_t const addressValue) {
24+
return InitSensor(addressValue << 1);
25+
}
26+
27+
void VL53L1GpioInterface::VL53L1_Off() {
28+
digitalWrite(xshutdown, LOW);
29+
delay(10);
30+
}
31+
32+
int VL53L1GpioInterface::end() {
33+
pinMode(xshutdown, INPUT);
34+
return 0;
35+
}
36+
37+
void VL53L1GpioInterface::VL53L1_On() {
38+
digitalWrite(xshutdown, HIGH);
39+
delay(10);
40+
}
41+
42+
int VL53L1GpioInterface::begin() {
43+
pinMode(xshutdown, OUTPUT);
44+
return 0;
45+
}

0 commit comments

Comments
 (0)