22 Commits

Author SHA1 Message Date
Alex Shepherd
7e3b3e3469 Changed how INPUT_PULLUP is done to be compatibile with ESP32
Bumped version to 2.0.6
2020-10-20 09:42:00 +13:00
Alex Shepherd
a09c6b1002 bumped NMRADCC_VERSION to 205 2020-06-02 09:50:12 +12:00
Alex Shepherd
29bc2aa1c0 default to DISABLE_OUTPUTS_IDLE uncommented 2020-06-01 19:04:56 +12:00
Alex Shepherd
3abb683d8d improved DCCInterface_TurntableControl example to:
- Added selectable Movement Strategy: Positive Direction Only, Negative Direction Only, Shortest Distance
- Made Disabling Output on Idle optional
2020-06-01 18:49:00 +12:00
Alex Shepherd
f3da07c739 added Franz-Peter's changes to pass NMRA Tests ( always search for preamble ) 2020-05-31 20:14:28 +12:00
Alex Shepherd
dead808af2 added work-around for ATtiny1614 range of chips that have a bug in: eeprom_is_ready() 2020-05-30 16:14:27 +12:00
Alex Shepherd
38194c0148 Correct the inconsistent license text in the source files to be the Lesser GPL as was intended by the LICENSE file and the repo classification 2020-02-03 22:16:00 +13:00
Alex Shepherd
7819716ac0 manually merged in Franz-Peter's changes as his PR#30 pull-request had too many differences to merge once I'd already solved the advancedAck issue.
This commit contains the remaining relevant changes after doing a manual diff of the library files
2020-01-10 17:33:09 +13:00
Alex Shepherd
5349a21e2e fix bug caused when AdvancedCVAck logic was added as the CV Programming code was shared between Service Mode and Ops Mode programming. 2020-01-10 15:30:30 +13:00
Alex Shepherd
36418f4d66 fixed a bug with the Extended DCC Packet Decoder Reset command was only handling the Hard Reset but not the Soft Reset 2019-11-23 14:25:28 +13:00
Alex Shepherd
a8081526ba Merge commit '71bb657e3a7d80edc096094564f5d279e8a1fb69'
* commit '71bb657e3a7d80edc096094564f5d279e8a1fb69':
  Esp32 iram attr (#26)

The merge adds extra STM32 handling

Als manually resolved the conflict around the type for lastMicros, to now be:

	    static unsigned int  lastMicros = 0;

Howefully this does the right thing on each platform
2019-10-28 18:58:02 +13:00
Alex Shepherd
7063e4e7e7 Merge branch 'master' of https://github.com/mrrwa/NmraDcc
* 'master' of https://github.com/mrrwa/NmraDcc:
  Update NmraDcc.cpp
  Update NmraDcc.cpp (#28)
2019-08-19 19:08:28 +12:00
Alex Shepherd
d8fb99ed91 Update NmraDcc.cpp
Added some more comments to the ServiceMode programming DCC Packet comparison logic
2019-08-19 19:04:51 +12:00
Roeland
794128fe4b Update NmraDcc.cpp (#28)
Changed the Service Mode duplicate packet comparison to only compare the packet Size and  Data fields and ignore the number of Preamble bits as some command stations appear to send different numbers of preamble bits for otherwise duplicated packets, so we detecting the required two identical packets in sequence and performing the ServiceMode Operation
2019-08-19 18:59:29 +12:00
Alex Shepherd
1542a6fe6c Merge branch 'AdvancedCVAck'
* AdvancedCVAck:
  corrected method description
  split out ServiceMode ackCV from Ops Mode AdvancedCVAck as doing a ackCV in Ops Mode is wrong and adds 6ms busy delay add cache of CV29 value
2019-08-09 08:20:45 +12:00
Alex Shepherd
828b1feaba corrected method description 2019-08-09 08:19:53 +12:00
Bob Jacobsen
462025b9fe Comment update (#27)
Incredibly trivial update to comment noticed while reading code.
2019-08-06 09:10:21 +12:00
Alex Shepherd
f3a2b87693 split out ServiceMode ackCV from Ops Mode AdvancedCVAck as doing a ackCV in Ops Mode is wrong and adds 6ms busy delay
add cache of CV29 value
2019-08-06 00:22:04 +12:00
Alex Shepherd
e06f6b3bce bumped version to 2.0.2 2019-08-05 21:34:48 +12:00
Alex Shepherd
6a7e206032 reverted changes around lastMicros 2019-08-05 21:30:39 +12:00
Alex Shepherd
6d12e6cd3f added conditional compilation for ESP8266 to add ICACHE_RAM_ATTR to ExternalInterruptHandler
changed storage for Micros to unsigned long
2019-05-25 15:52:55 +12:00
Alex Shepherd
b249762550 changed the version to 201 in the header 2019-05-02 21:42:59 +12:00
5 changed files with 1820 additions and 1594 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.development
*.zip

File diff suppressed because it is too large Load Diff

View File

@@ -2,11 +2,21 @@
//
// Model Railroading with Arduino - NmraDcc.h
//
// Copyright (c) 2008 - 2018 Alex Shepherd
// Copyright (c) 2008 - 2020 Alex Shepherd
//
// 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
// 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
//
//------------------------------------------------------------------------
//
@@ -44,12 +54,10 @@
#include "WProgram.h"
#endif
#include "EEPROM.h"
#ifndef NMRADCC_IS_IN
#define NMRADCC_IS_IN
#define NMRADCC_VERSION 201 // Version 2.0.1
#define NMRADCC_VERSION 206 // Version 2.0.6
#define MAX_DCC_MESSAGE_LEN 6 // including XOR-Byte
@@ -99,7 +107,7 @@ typedef struct
#define CV_29_CONFIG 29
#if defined(ESP32)
#include <esp_log.h>
#include <esp_spi_flash.h>
#define MAXCV SPI_FLASH_SEC_SIZE
#elif defined(ESP8266)
#include <spi_flash.h>
@@ -118,7 +126,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_ADV_ACK = 0b00001000, /** bit 3: ACK, Advanced Acknowledge mode enabled if 1, disabled if 0 */
CV29_RAILCOM_ENABLE = 0b00001000, /** bit 3: BiDi ( RailCom ) is active */
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 */
@@ -703,6 +711,17 @@ extern void notifyCVResetFactoryDefault(void) __attribute__ ((weak));
* None
*/
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.
*
* Inputs:
* None
* *
* Returns:
* None
*/
extern void notifyAdvancedCVAck(void) __attribute__ ((weak));
/*+
* notifyServiceMode(bool) Called when state of 'inServiceMode' changes
*

View File

@@ -1,8 +1,8 @@
// DCC Stepper Motor Controller ( A4988 ) Example for Model Railroad Turntable Control
//
// See: https://www.dccinterface.com/how-to/assemblyguide/
// See: https://www.dccinterface.com/product/arduino-model-railway-dcc-stepper-motor-controller-a4988-assembled/
//
// Author: Alex Shepherd 2017-12-04
// Author: Alex Shepherd 2020-06-01
//
// This example requires two Arduino Libraries:
//
@@ -22,12 +22,22 @@
// The lines below define the pins used to connect to the A4988 driver module
#define A4988_STEP_PIN 4
#define A4988_DIRECTION_PIN 5
// Uncomment the next line to enable Powering-Off the Stepper when its not running to reduce heating the motor and driver
#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
// 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
@@ -92,6 +102,9 @@ 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))
@@ -104,11 +117,12 @@ 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("notifyDccAccTurnoutOutput: ") ;
Serial.print(F("notifyDccAccTurnoutOutput: "));
Serial.print(Addr,DEC) ;
Serial.print(',');
Serial.print(Direction,DEC) ;
@@ -131,23 +145,50 @@ void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t Output
#ifdef A4988_ENABLE_PIN
stepper1.enableOutputs();
#endif
if (Direction)
{
Serial.println(turnoutPositions[i].positionFront, DEC);
stepper1.moveTo(turnoutPositions[i].positionFront);
break;
}
int newStep;
if(Direction)
newStep = turnoutPositions[i].positionFront;
else
{
Serial.println(turnoutPositions[i].positionBack, DEC);
stepper1.moveTo(turnoutPositions[i].positionBack);
break;
}
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;
}
}
};
#ifdef A4988_ENABLE_PIN
#ifdef DISABLE_OUTPUTS_IDLE
bool lastIsRunningState ;
#endif
@@ -161,8 +202,12 @@ 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
}
@@ -173,14 +218,19 @@ 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)
{
Serial.println(F("Found Home Position - Setting Current Position to 0"));
stepper1.stop();
stepper1.setCurrentPosition(0);
Serial.println(F("Found Home Position - Setting Current Position to 0"));
return true;
}
else
@@ -210,28 +260,32 @@ 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("DCC Addr: ");
Serial.print(F("DCC Addr: "));
Serial.print(turnoutPositions[i].dccAddress);
Serial.print(" Front: ");
Serial.print(F(" Front: "));
Serial.print(turnoutPositions[i].positionFront);
Serial.print(" Back: ");
Serial.print(F(" 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);
}
@@ -245,7 +299,7 @@ void loop()
// Process the Stepper Library
stepper1.run();
#ifdef A4988_ENABLE_PIN
#ifdef DISABLE_OUTPUTS_IDLE
if(stepper1.isRunning() != lastIsRunningState)
{
lastIsRunningState = stepper1.isRunning();

View File

@@ -1,5 +1,5 @@
name=NmraDcc
version=2.0.1
version=2.0.6
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