Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
||||||
@@ -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": "*"
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user