2020-01-30 09:28:01 +00:00
|
|
|
#include "./motor.h"
|
2020-01-31 09:36:36 +00:00
|
|
|
#include "include/config.h"
|
2020-02-02 11:19:24 +00:00
|
|
|
#include "./debug.h"
|
2020-01-31 09:36:36 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
|
|
void motorInit()
|
|
|
|
{
|
|
|
|
pinMode(Config::MotorPinSleep, OUTPUT);
|
|
|
|
digitalWrite(Config::MotorPinSleep, LOW);
|
|
|
|
|
|
|
|
pinMode(Config::MotorPinPWM, OUTPUT);
|
|
|
|
digitalWrite(Config::MotorPinPWM, LOW);
|
|
|
|
|
|
|
|
pinMode(Config::MotorPinDirection, OUTPUT);
|
|
|
|
pinMode(Config::MotorPinCurrentSensing, INPUT);
|
2020-02-02 11:19:24 +00:00
|
|
|
|
|
|
|
// See motorIsOvercurrent
|
|
|
|
analogReference(INTERNAL);
|
|
|
|
delay(10);
|
2020-01-31 09:36:36 +00:00
|
|
|
}
|
2020-01-30 09:28:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void motorStart(MotorDirection direction)
|
|
|
|
{
|
2020-02-02 11:32:12 +00:00
|
|
|
//dl("[ MOTOR ] Starting in direction: "); dln((uint8_t)direction);
|
2020-02-02 11:19:24 +00:00
|
|
|
|
2020-01-31 09:36:36 +00:00
|
|
|
digitalWrite(Config::MotorPinDirection, direction == MotorDirection::Up ? HIGH : LOW);
|
|
|
|
digitalWrite(Config::MotorPinSleep, HIGH);
|
|
|
|
digitalWrite(Config::MotorPinPWM, HIGH);
|
2020-01-30 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void motorStop()
|
|
|
|
{
|
2020-02-02 11:32:12 +00:00
|
|
|
//dln("[ MOTOR ] Stopping");
|
2020-02-02 11:19:24 +00:00
|
|
|
|
2020-01-31 09:36:36 +00:00
|
|
|
digitalWrite(Config::MotorPinPWM, LOW);
|
|
|
|
digitalWrite(Config::MotorPinSleep, LOW);
|
2020-02-01 19:54:05 +00:00
|
|
|
digitalWrite(Config::MotorPinDirection, LOW);
|
2020-01-30 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-01 15:55:33 +00:00
|
|
|
bool motorIsOvercurrent()
|
2020-01-30 09:28:01 +00:00
|
|
|
{
|
2020-02-02 11:19:24 +00:00
|
|
|
// The Polugu G2 module outputs 20 mV/A plus a 50 mV offset
|
|
|
|
// The analog reference has been set to internal, which is 1.1v for the ATMega328P,
|
|
|
|
// divided by 1024 units is 1.074mV per unit. This means we can measure up to 52.5A.
|
|
|
|
// I sure hope we never reach that value :-)
|
|
|
|
auto value = analogRead(Config::MotorPinCurrentSensing);
|
|
|
|
float voltage = (float)value * (1100.0 / 1024.0);
|
|
|
|
float current = (voltage - 50) / 20;
|
2020-01-30 09:28:01 +00:00
|
|
|
|
2020-02-02 11:19:24 +00:00
|
|
|
dl("[ MOTOR ] Measured current: value = "); dl(value); dl(", V = "); dl(voltage); dl(", A = "); dln(current);
|
2020-01-30 09:28:01 +00:00
|
|
|
|
2020-02-02 11:19:24 +00:00
|
|
|
return current >= Config::MotorCurrentLimit;
|
|
|
|
}
|