1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 16:16:13 +01:00
CommandStation-EX/MotorDriver.cpp

136 lines
4.2 KiB
C++
Raw Normal View History

2020-08-14 23:54:12 +02:00
/*
* © 2020, Chris Harlow. All rights reserved.
*
* This file is part of Asbelos DCC API
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "MotorDriver.h"
#include "DIAG.h"
2021-02-04 11:43:13 +01:00
#define setHIGH(fastpin) *fastpin.inout |= fastpin.maskHIGH
#define setLOW(fastpin) *fastpin.inout &= fastpin.maskLOW
#define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH)
2021-02-02 12:30:35 +01:00
#define isLOW(fastpin) (!isHIGH(fastpin))
MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) {
2020-10-31 11:27:29 +01:00
powerPin=power_pin;
2021-02-02 12:30:35 +01:00
getFastPin(F("POWER"),powerPin,fastPowerPin);
pinMode(powerPin, OUTPUT);
2020-10-31 11:27:29 +01:00
signalPin=signal_pin;
getFastPin(F("SIG"),signalPin,fastSignalPin);
pinMode(signalPin, OUTPUT);
2020-10-31 11:27:29 +01:00
signalPin2=signal_pin2;
if (signalPin2!=UNUSED_PIN) {
dualSignal=true;
getFastPin(F("SIG2"),signalPin2,fastSignalPin2);
pinMode(signalPin2, OUTPUT);
}
else dualSignal=false;
2020-10-31 11:27:29 +01:00
brakePin=brake_pin;
2021-02-02 12:30:35 +01:00
if (brake_pin!=UNUSED_PIN){
invertBrake=brake_pin < 0;
brakePin=invertBrake ? 0-brake_pin : brake_pin;
getFastPin(F("BRAKE"),brakePin,fastBrakePin);
pinMode(brakePin, OUTPUT);
setBrake(false);
}
2021-02-02 12:30:35 +01:00
else brakePin=UNUSED_PIN;
2020-10-31 11:27:29 +01:00
currentPin=current_pin;
pinMode(currentPin, INPUT);
2020-10-31 11:27:29 +01:00
faultPin=fault_pin;
2021-02-02 12:30:35 +01:00
if (faultPin != UNUSED_PIN) {
2021-02-04 11:43:13 +01:00
getFastPin(F("FAULT"),faultPin, 1 /*input*/, fastFaultPin);
2021-02-02 12:30:35 +01:00
pinMode(faultPin, INPUT);
}
senseFactor=sense_factor;
2020-10-31 11:27:29 +01:00
tripMilliamps=trip_milliamps;
rawCurrentTripValue=(int)(trip_milliamps / sense_factor);
2020-08-14 23:54:12 +02:00
}
void MotorDriver::setPower(bool on) {
2021-02-02 12:30:35 +01:00
if (on) {
2020-11-01 10:16:29 +01:00
// toggle brake before turning power on - resets overcurrent error
// on the Pololu board if brake is wired to ^D2.
setBrake(true);
setBrake(false);
2021-02-02 12:30:35 +01:00
setHIGH(fastPowerPin);
2020-11-01 10:16:29 +01:00
}
2021-02-02 12:30:35 +01:00
else setLOW(fastPowerPin);
2020-08-14 23:54:12 +02:00
}
// setBrake applies brake if on == true. So to get
// voltage from the motor bride one needs to do a
// setBrake(false).
// If the brakePin is negative that means the sense
// of the brake pin on the motor bridge is inverted
// (HIGH == release brake) and setBrake does
// compensate for that.
//
void MotorDriver::setBrake(bool on) {
2021-02-02 12:30:35 +01:00
if (brakePin == UNUSED_PIN) return;
if (on ^ invertBrake) setHIGH(fastBrakePin);
else setLOW(fastBrakePin);
2020-08-14 23:54:12 +02:00
}
void MotorDriver::setSignal( bool high) {
if (high) {
setHIGH(fastSignalPin);
if (dualSignal) setLOW(fastSignalPin2);
}
else {
setLOW(fastSignalPin);
if (dualSignal) setHIGH(fastSignalPin2);
}
2020-08-14 23:54:12 +02:00
}
int MotorDriver::getCurrentRaw() {
2021-02-04 11:43:13 +01:00
int current = analogRead(currentPin);
2021-02-02 12:30:35 +01:00
if (faultPin != UNUSED_PIN && isLOW(fastFaultPin) && isHIGH(fastPowerPin))
2021-02-04 11:43:13 +01:00
return -current;
return current;
2020-08-14 23:54:12 +02:00
// IMPORTANT: This function can be called in Interrupt() time within the 56uS timer
// The default analogRead takes ~100uS which is catastrphic
// so DCCTimer has set the sample time to be much faster.
2020-08-14 23:54:12 +02:00
}
unsigned int MotorDriver::raw2mA( int raw) {
2020-08-14 23:54:12 +02:00
return (unsigned int)(raw * senseFactor);
}
int MotorDriver::mA2raw( unsigned int mA) {
return (int)(mA / senseFactor);
}
2021-02-04 11:43:13 +01:00
void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & result) {
DIAG(F("\nMotorDriver %S Pin=%d,"),type,pin);
uint8_t port = digitalPinToPort(pin);
2021-02-04 11:43:13 +01:00
if (input)
result.inout = portInputRegister(port);
else
result.inout = portOutputRegister(port);
result.maskHIGH = digitalPinToBitMask(pin);
result.maskLOW = ~result.maskHIGH;
2021-02-04 11:43:13 +01:00
DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x\n"),port, result.inout,input,result.maskHIGH);
}