Init: Rattenfalle

This commit is contained in:
2024-01-27 23:32:45 +01:00
committed by Jakob Gegeniger
parent 3706823709
commit e82dd3f3a2
18 changed files with 1122 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
/**
* @file DcMotor.hpp
* @author Gustice
* @brief Simple Driver implementation for DRVxxxx Derivatives from Texas instruments
* DRV8848
*
* @copyright Copyright (c) 2024
*/
#include <Arduino.h>
#include <array>
class DcMotor {
static constexpr int FullScale = 256;
public:
struct Pins {
std::array<int, 2> PwmA; // PWM control
int ErrorIn; // Error Feedback from driver
int Sleep; // Sleep control (to enter double mode)
int Ceil; // Maximum speed control
int ErrOut; // Maximum speed control
};
enum State {
Startup = 0,
Idle,
Right,
Left,
};
DcMotor(Pins pins) : _pins(pins) {
pinMode(_pins.PwmA[0], OUTPUT);
pinMode(_pins.PwmA[1], OUTPUT);
pinMode(_pins.ErrorIn, INPUT);
pinMode(_pins.Sleep, OUTPUT);
pinMode(_pins.Ceil, INPUT);
pinMode(_pins.ErrOut, OUTPUT);
// sleep to enter double current mode
delay(5);
digitalWrite(_pins.Sleep, LOW);
delay(5); // @todo double after successful operation
digitalWrite(_pins.Sleep, HIGH);
// set to break
analogWrite(_pins.PwmA[0], FullScale);
analogWrite(_pins.PwmA[1], FullScale);
}
void stop() {
analogWrite(_pins.PwmA[0], FullScale);
analogWrite(_pins.PwmA[1], FullScale);
_state = Idle;
}
void startRight() {
int ceil = analogRead(_pins.Ceil) / 4;
_state = Right;
analogWrite(_pins.PwmA[0], FullScale);
analogWrite(_pins.PwmA[1], FullScale - ceil);
}
void startLeft() {
int ceil = analogRead(_pins.Ceil) / 4;
_state = Left;
analogWrite(_pins.PwmA[0], FullScale - ceil);
analogWrite(_pins.PwmA[1], FullScale);
}
void tick() {
int ceil = analogRead(_pins.Ceil) / 4;
switch (_state) {
case Right:
analogWrite(_pins.PwmA[1], FullScale - ceil);
break;
case Left:
analogWrite(_pins.PwmA[0], FullScale - ceil);
break;
default:
break;
}
int nErr = digitalRead(_pins.ErrorIn);
digitalWrite(_pins.ErrOut, nErr == 0);
}
private:
Pins _pins;
State _state = Startup;
};
+46
View File
@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
+73
View File
@@ -0,0 +1,73 @@
/**
* @file RotatingIndex.hpp
* @author Gustice
* @brief Classes for typical index manipulation
*/
#pragma once
/**
* @brief Index with automatic revolution back to start position
* @details Useful for Buffer-Pointer, or similar tasks
*
* @tparam T Numeric type
*/
template <typename T = int>
class RotatingIndex {
public:
/// @brief Value at witch the index rotates back to beginning
const T MaxIndex;
/**
* @brief Constructor for rotating index
* @brief output index will be set to 0 if incremented value reaches Maximum
* @param max Maximum index for overflow-condition
*/
RotatingIndex(T max) : MaxIndex(max) {
_index = 0;
}
/**
* @brief Get the current index and increment index afterwards
* @return T current index
*/
T GetIndexAndIncrement() {
auto retVal = _index;
_index++;
if (_index >= MaxIndex) {
_index = 0;
}
return retVal;
};
/**
* @brief Get the current index without incrementing the index
* @return T current index
*/
T GetIndex() const {
return _index;
}
/**
* @brief Increments index and retuns if index has been reset
*/
bool IncrementAndCheckIfRevolves() {
_index++;
if (_index >= MaxIndex) {
_index = 0;
return true;
}
return false;
}
/**
* @brief Reset Index to 0
*/
void ResetIndex() {
_index = 0;
}
private:
/// Current Index
T _index;
};