commit 02f9a4235384bfac7d9ec38d4dffd4fd398c52ab Author: Alexander Klein Date: Fri Feb 10 08:57:50 2023 +0100 Initial commit diff --git a/include/motorControl.h b/include/motorControl.h new file mode 100644 index 0000000..c2a1d8e --- /dev/null +++ b/include/motorControl.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "motor.h" + +class MotorControl : public Motor{ + public: + MotorControl(ShiftRegister* shiftRegister, uint8_t pwmPin, + uint8_t dirPinA, uint8_t dirPinB, uint8_t endstopPinA, + uint8_t endstopPinB); + + void loop(); + bool start() override; + + bool getEndStopLeft(); + bool getEndStopRight(); + + private: + Direction endstopTouched = Direction::None; + bool readPin(uint8_t pin); + + uint8_t endstopPinA; + uint8_t endstopPinB; + + uint32_t lastMillis = 0; + uint8_t loopDelay = 20; +}; diff --git a/library.json b/library.json new file mode 100644 index 0000000..fa1d99e --- /dev/null +++ b/library.json @@ -0,0 +1,26 @@ +{ + "name": "MotorControl", + "version": "1.0.0", + "description": "Control Motor with end stops.", + "keywords": "motor, direction, engine, end stops", + "repository": + { + "type": "git", + "url": "https://git.kleiax.de/PlatformIO-Libs/MotorControl.git" + }, + "authors": + [ + { + "name": "Alexander Klein", + "email": "alex@kleiax.de", + "url": "https://www.kleiax.de/contact" + } + ], + "license": "MIT", + "homepage": "https://www.kleiax.de/", + "dependencies": { + "external-repo" : "https://git.kleiax.de/PlatformIO-Libs/Motor.git#v1.0" + }, + "frameworks": "*", + "platforms": "*" + } \ No newline at end of file diff --git a/src/motorControl.cpp b/src/motorControl.cpp new file mode 100644 index 0000000..d7169f6 --- /dev/null +++ b/src/motorControl.cpp @@ -0,0 +1,57 @@ +#include "motorControl.h" + +MotorControl::MotorControl(ShiftRegister* shiftRegister, uint8_t pwmPin, + uint8_t dirPinA, uint8_t dirPinB, uint8_t endstopPinA, + uint8_t endstopPinB) + : Motor(shiftRegister, pwmPin, dirPinA, dirPinB) { + pinMode(this->endstopPinA, INPUT); + pinMode(this->endstopPinB, INPUT); + this->endstopPinA = endstopPinA; + this->endstopPinB = endstopPinB; +} + +void MotorControl::loop() { + if (millis() - this->lastMillis < this->loopDelay) + return; + + if (readPin(this->endstopPinA)) + this->endstopTouched = Direction::Left; + else if (readPin(this->endstopPinB)) + this->endstopTouched = Direction::Right; + else + this->endstopTouched = Direction::None; + + if (this->direction == this->endstopTouched) + this->stop(); + + this->lastMillis = millis(); +} + + + +bool MotorControl::start() { + if (this->direction == this->endstopTouched) + return false; + + this->configureMotor(); + return true; +} + +bool MotorControl::getEndStopLeft() { + return readPin(this->endstopPinA); +} + +bool MotorControl::getEndStopRight() { + return readPin(this->endstopPinB); +} + +bool MotorControl::readPin(uint8_t pin) { + bool res = false; + + if (pin == 20 || pin == 21) + res = (analogRead(pin) > 512); + else + res = digitalRead(pin); + + return res; +} \ No newline at end of file