Compare commits
1 Commits
2.0.6
...
ESP32-IRAM
Author | SHA1 | Date | |
---|---|---|---|
|
4b175e9229 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
.development
|
||||
*.zip
|
3268
NmraDcc.cpp
3268
NmraDcc.cpp
File diff suppressed because it is too large
Load Diff
29
NmraDcc.h
29
NmraDcc.h
@@ -2,21 +2,11 @@
|
||||
//
|
||||
// Model Railroading with Arduino - NmraDcc.h
|
||||
//
|
||||
// Copyright (c) 2008 - 2020 Alex Shepherd
|
||||
// Copyright (c) 2008 - 2018 Alex Shepherd
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
// This source file is subject of the GNU general public license 2,
|
||||
// that is available at the world-wide-web at
|
||||
// http://www.gnu.org/licenses/gpl.txt
|
||||
//
|
||||
//------------------------------------------------------------------------
|
||||
//
|
||||
@@ -54,10 +44,12 @@
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include "EEPROM.h"
|
||||
|
||||
#ifndef NMRADCC_IS_IN
|
||||
#define NMRADCC_IS_IN
|
||||
|
||||
#define NMRADCC_VERSION 206 // Version 2.0.6
|
||||
#define NMRADCC_VERSION 201 // Version 2.0.1
|
||||
|
||||
#define MAX_DCC_MESSAGE_LEN 6 // including XOR-Byte
|
||||
|
||||
@@ -126,7 +118,7 @@ typedef enum {
|
||||
CV29_LOCO_DIR = 0b00000001, /** bit 0: Locomotive Direction: "0" = normal, "1" = reversed */
|
||||
CV29_F0_LOCATION = 0b00000010, /** bit 1: F0 location: "0" = bit 4 in Speed and Direction instructions, "1" = bit 4 in function group one instruction */
|
||||
CV29_APS = 0b00000100, /** bit 2: Alternate Power Source (APS) "0" = NMRA Digital only, "1" = Alternate power source set by CV12 */
|
||||
CV29_RAILCOM_ENABLE = 0b00001000, /** bit 3: BiDi ( RailCom ) is active */
|
||||
CV29_ADV_ACK = 0b00001000, /** bit 3: ACK, Advanced Acknowledge mode enabled if 1, disabled if 0 */
|
||||
CV29_SPEED_TABLE_ENABLE = 0b00010000, /** bit 4: STE, Speed Table Enable, "0" = values in CVs 2, 4 and 6, "1" = Custom table selected by CV 25 */
|
||||
CV29_EXT_ADDRESSING = 0b00100000, /** bit 5: "0" = one byte addressing, "1" = two byte addressing */
|
||||
CV29_OUTPUT_ADDRESS_MODE = 0b01000000, /** bit 6: "0" = Decoder Address Mode "1" = Output Address Mode */
|
||||
@@ -712,8 +704,9 @@ extern void notifyCVResetFactoryDefault(void) __attribute__ ((weak));
|
||||
*/
|
||||
extern void notifyCVAck(void) __attribute__ ((weak));
|
||||
/*+
|
||||
* notifyAdvancedCVAck() Called when a CV write must be acknowledged via Advanced Acknowledgement.
|
||||
* This callback must send the Advanced Acknowledgement via RailComm.
|
||||
* notifyAdvancedCVAck() Called when a CV write must be acknowledged.
|
||||
* This callback must increase the current drawn by this
|
||||
* decoder by at least 60mA for 6ms +/- 1ms.
|
||||
*
|
||||
* Inputs:
|
||||
* None
|
||||
|
@@ -1,8 +1,8 @@
|
||||
// DCC Stepper Motor Controller ( A4988 ) Example for Model Railroad Turntable Control
|
||||
//
|
||||
// See: https://www.dccinterface.com/product/arduino-model-railway-dcc-stepper-motor-controller-a4988-assembled/
|
||||
// See: https://www.dccinterface.com/how-to/assemblyguide/
|
||||
//
|
||||
// Author: Alex Shepherd 2020-06-01
|
||||
// Author: Alex Shepherd 2017-12-04
|
||||
//
|
||||
// This example requires two Arduino Libraries:
|
||||
//
|
||||
@@ -22,22 +22,12 @@
|
||||
// The lines below define the pins used to connect to the A4988 driver module
|
||||
#define A4988_STEP_PIN 4
|
||||
#define A4988_DIRECTION_PIN 5
|
||||
#define A4988_ENABLE_PIN 6
|
||||
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
// Uncomment the next line to enable Powering-Off the Stepper when its not running to reduce heating the motor and driver
|
||||
#define DISABLE_OUTPUTS_IDLE
|
||||
#endif
|
||||
|
||||
// By default the stepper motor will move the shortest distance to the desired position.
|
||||
// If you need the turntable to only move in the Positive/Increasing or Negative/Decreasing step numbers to better handle backlash in the mechanism
|
||||
// Then uncomment the appropriate line below
|
||||
//#define ALWAYS_MOVE_POSITIVE
|
||||
//#define ALWAYS_MOVE_NEGATIVE
|
||||
#define A4988_ENABLE_PIN 6
|
||||
|
||||
// The lines below define the stepping speed and acceleration, which you may need to tune for your application
|
||||
#define STEPPER_MAX_SPEED 800 // Sets the maximum permitted speed
|
||||
#define STEPPER_ACCELARATION 1000 // Sets the acceleration/deceleration rate
|
||||
#define STEPPER_ACCELARATION 1000 // Sets the acceleration/deceleration rate
|
||||
#define STEPPER_SPEED 300 // Sets the desired constant speed for use with runSpeed()
|
||||
|
||||
// The line below defines the number of "Full Steps" your stepper motor does for a full rotation
|
||||
@@ -102,9 +92,6 @@ TurnoutPosition turnoutPositions[] = {
|
||||
// --------------------------------------------------------------------------------------------
|
||||
// You shouldn't need to edit anything below this line unless you're needing to make big changes... ;)
|
||||
// --------------------------------------------------------------------------------------------
|
||||
#if defined(ALWAYS_MOVE_POSITIVE) && defined(ALWAYS_MOVE_NEGATIVE)
|
||||
#error ONLY uncomment one of ALWAYS_MOVE_POSITIVE or ALWAYS_MOVE_NEGATIVE but NOT both
|
||||
#endif
|
||||
|
||||
#define MAX_TURNOUT_POSITIONS (sizeof(turnoutPositions) / sizeof(TurnoutPosition))
|
||||
|
||||
@@ -117,12 +104,11 @@ NmraDcc Dcc ;
|
||||
// Variables to store the last DCC Turnout message Address and Direction
|
||||
uint16_t lastAddr = 0xFFFF ;
|
||||
uint8_t lastDirection = 0xFF;
|
||||
int lastStep = 0;
|
||||
|
||||
// This function is called whenever a normal DCC Turnout Packet is received
|
||||
void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t OutputPower )
|
||||
{
|
||||
Serial.print(F("notifyDccAccTurnoutOutput: "));
|
||||
Serial.print("notifyDccAccTurnoutOutput: ") ;
|
||||
Serial.print(Addr,DEC) ;
|
||||
Serial.print(',');
|
||||
Serial.print(Direction,DEC) ;
|
||||
@@ -145,50 +131,23 @@ void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t Output
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
stepper1.enableOutputs();
|
||||
#endif
|
||||
|
||||
int newStep;
|
||||
if(Direction)
|
||||
newStep = turnoutPositions[i].positionFront;
|
||||
if (Direction)
|
||||
{
|
||||
Serial.println(turnoutPositions[i].positionFront, DEC);
|
||||
stepper1.moveTo(turnoutPositions[i].positionFront);
|
||||
break;
|
||||
}
|
||||
else
|
||||
newStep = turnoutPositions[i].positionBack;
|
||||
|
||||
Serial.print(newStep, DEC);
|
||||
|
||||
Serial.print(F(" Last Step: "));
|
||||
Serial.print(lastStep, DEC);
|
||||
|
||||
int diffStep = newStep - lastStep;
|
||||
Serial.print(F(" Diff Step: "));
|
||||
Serial.print(diffStep, DEC);
|
||||
|
||||
#if defined ALWAYS_MOVE_POSITIVE
|
||||
Serial.print(F(" Positive"));
|
||||
if(diffStep < 0)
|
||||
diffStep += FULL_TURN_STEPS;
|
||||
|
||||
#elif defined ALWAYS_MOVE_NEGATIVE
|
||||
Serial.print(F(" Negative"));
|
||||
if(diffStep > 0)
|
||||
diffStep -= FULL_TURN_STEPS;
|
||||
#else
|
||||
if(diffStep > HALF_TURN_STEPS)
|
||||
diffStep = diffStep - FULL_TURN_STEPS;
|
||||
|
||||
else if(diffStep < -HALF_TURN_STEPS)
|
||||
diffStep = diffStep + FULL_TURN_STEPS;
|
||||
#endif
|
||||
|
||||
Serial.print(F(" Move: "));
|
||||
Serial.println(diffStep, DEC);
|
||||
stepper1.move(diffStep);
|
||||
|
||||
lastStep = newStep;
|
||||
break;
|
||||
{
|
||||
Serial.println(turnoutPositions[i].positionBack, DEC);
|
||||
stepper1.moveTo(turnoutPositions[i].positionBack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef DISABLE_OUTPUTS_IDLE
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
bool lastIsRunningState ;
|
||||
#endif
|
||||
|
||||
@@ -202,12 +161,8 @@ void setupStepperDriver()
|
||||
stepper1.setMaxSpeed(STEPPER_MAX_SPEED); // Sets the maximum permitted speed
|
||||
stepper1.setAcceleration(STEPPER_ACCELARATION); // Sets the acceleration/deceleration rate
|
||||
stepper1.setSpeed(STEPPER_SPEED); // Sets the desired constant speed for use with runSpeed()
|
||||
|
||||
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
stepper1.enableOutputs();
|
||||
#endif
|
||||
|
||||
#ifdef DISABLE_OUTPUTS_IDLE
|
||||
lastIsRunningState = stepper1.isRunning();
|
||||
#endif
|
||||
}
|
||||
@@ -218,19 +173,14 @@ bool moveToHomePosition()
|
||||
|
||||
pinMode(HOME_SENSOR_PIN, INPUT_PULLUP);
|
||||
|
||||
#ifdef ALWAYS_MOVE_NEGATIVE
|
||||
stepper1.move(0 - (FULL_TURN_STEPS * 2));
|
||||
#else
|
||||
stepper1.move(FULL_TURN_STEPS * 2);
|
||||
#endif
|
||||
while(digitalRead(HOME_SENSOR_PIN) != HOME_SENSOR_ACTIVE_STATE)
|
||||
stepper1.run();
|
||||
|
||||
if(digitalRead(HOME_SENSOR_PIN) == HOME_SENSOR_ACTIVE_STATE)
|
||||
{
|
||||
stepper1.stop();
|
||||
stepper1.setCurrentPosition(0);
|
||||
Serial.println(F("Found Home Position - Setting Current Position to 0"));
|
||||
stepper1.setCurrentPosition(0);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@@ -260,32 +210,28 @@ void setup()
|
||||
Serial.print(F("Full Rotation Steps: "));
|
||||
Serial.println(FULL_TURN_STEPS);
|
||||
|
||||
Serial.print(F("Movement Strategy: "));
|
||||
#if defined ALWAYS_MOVE_POSITIVE
|
||||
Serial.println(F("Positive Direction Only"));
|
||||
#elif defined ALWAYS_MOVE_NEGATIVE
|
||||
Serial.println(F("Negative Direction Only"));
|
||||
#else
|
||||
Serial.println(F("Shortest Distance"));
|
||||
#endif
|
||||
|
||||
for(uint8_t i = 0; i < MAX_TURNOUT_POSITIONS; i++)
|
||||
{
|
||||
Serial.print(F("DCC Addr: "));
|
||||
Serial.print("DCC Addr: ");
|
||||
Serial.print(turnoutPositions[i].dccAddress);
|
||||
|
||||
Serial.print(F(" Front: "));
|
||||
Serial.print(" Front: ");
|
||||
Serial.print(turnoutPositions[i].positionFront);
|
||||
|
||||
Serial.print(F(" Back: "));
|
||||
Serial.print(" Back: ");
|
||||
Serial.println(turnoutPositions[i].positionBack);
|
||||
}
|
||||
|
||||
setupStepperDriver();
|
||||
|
||||
if(moveToHomePosition());
|
||||
{
|
||||
setupDCCDecoder();
|
||||
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
stepper1.enableOutputs();
|
||||
#endif
|
||||
|
||||
// Fake a DCC Packet to cause the Turntable to move to Position 1
|
||||
notifyDccAccTurnoutOutput(POSITION_01_DCC_ADDRESS, 1, 1);
|
||||
}
|
||||
@@ -299,7 +245,7 @@ void loop()
|
||||
// Process the Stepper Library
|
||||
stepper1.run();
|
||||
|
||||
#ifdef DISABLE_OUTPUTS_IDLE
|
||||
#ifdef A4988_ENABLE_PIN
|
||||
if(stepper1.isRunning() != lastIsRunningState)
|
||||
{
|
||||
lastIsRunningState = stepper1.isRunning();
|
||||
|
@@ -1,5 +1,5 @@
|
||||
name=NmraDcc
|
||||
version=2.0.6
|
||||
version=2.0.2
|
||||
author=Alex Shepherd, Wolfgang Kuffer, Geoff Bunza, Martin Pischky, Franz-Peter Müller, Sven (littleyoda), Hans Tanner
|
||||
maintainer=Alex Shepherd <kiwi64ajs@gmail.com>
|
||||
sentence=Enables NMRA DCC Communication
|
||||
|
Reference in New Issue
Block a user