Heb je nog voortgang gemaakt met je baan?
/* A program to convert DCC commands to Canbus messages Copyright (C) Meino, all rights reserved This program 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, version 2 of the License. This program 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.*/#include <DCC_Decoder.h>#include <CanBus.h>//// Other pin's used//#define DCCPIN 2//// Definitions for the DCC interface//#define K_DCC_INTERRUPT 0//// The CanBus//CanBus *canBus;
//// DCC Accessory packet handler//void DCC_PacketHandler(int address, bool activate, byte data){ // // Convert NMRA packet address to a useable address // // This calculation seems to be a little weird. However it has to do with // the way NMRA has implemented these things. Accessory decoders have an // basic address and a sub address. // F.I. decoder address 1 means first decoder (address 1) sub address 0. // Decoder address 2 means first decoder (address 1) sub address 1. // Decoder address 5 means second decoder (address 2), sub address 0, etc. // So the basic decoder address is not a C like index but a counter like in // Cobol, it doesn't start with 0. // // // Start with the base address // address -= 1; address *= 4; // // Add de sub address // address += 1; address += (data & 0x06) >> 1; // // Do we perform a normal(-) or a switch(/) operation? // For a normal operation enable is true, for a switch operation // enable is false. // bool enable = (data & 0x01) ? 1 : 0; // This bit actual specifies which of the two coils need // to be activated. (or deactivated if activate is false, which is not used // by this code) DCC_ACC_MSG buf; buf.address = address; if (enable) { buf.direction = 0; } else { buf.direction = 1; } // // Send the received accessory command // canBus->sendMsg(DCC_ACC, sizeof(DCC_ACC_MSG), &buf);}
void setup(){ // // Setup and start the CanBus communication // static const int nrOfBuffers = 7; static const int waitTime = 4; canBus = new CanBus(DCC_INTERFACE_ID, new Mcp2515(10, waitTime, nrOfBuffers), nullptr); // Only a sender, 250msg/sec, 10 sendbuffers canBus->begin(); // // Setup the DCC packet handler // DCC.SetBasicAccessoryDecoderPacketHandler(DCC_PacketHandler, true); DCC.SetupDecoder(0x00, 0x00, K_DCC_INTERRUPT); }
void loop(){ // // Check for DCC packets // DCC.loop(); // // Activate the CanBus // canBus->heartBeat(); }
Overigens weet ik niet of jouw idee van signaal versterking gaat werken
/* A program to accept occupancy detector status from the Canbus and set the proper bit in the S88 shift register Copyright (C) Meino de Graaf, all rights reserved This program 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, version 2 of the License. This program 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. email: meino@innerside.demon.nl*/#include <CanBus.h>//// Pin layout is for the Arduino Uno//// Connections for S88 bus://// s88 pin 1 Data - Arduino pin 8 = Data_Out to Oommand Station, or pin 7 (Data in) from the next Arduino in the chain// s88 pin 2 GND - Arduino GND// s88 pin 3 Clock - Arduino pin 2, interrupt 0// s88 pin 4 PS - Arduino pin 3, interrupt 1// S88 pin 5 Reset (not used)// s88 pin 6 V+ - Arduino 5V (not used)//// Connections for S88N (RJ45)//// RJ45 pin 1 12V/5V (not used)// RJ45 pin 2 DATA - Arduino pin 8 Data_Out to Oommand Station, or pin 7 Data In from the next Arduino in the chain// RJ45 pin 3 GND - Arduino GND// RJ45 pin 4 CLOCK - Arduino pin 2, interrupt 0// RJ45 pin 5 GND - Arduino GND// RJ45 pin 6 PS - Arduino pin 3, interrupt 1// RJ45 pin 7 RESET (not used)// RJ45 pin 8 RAILDATA (not used)//#define S88_DATAOUT_PIN 8#define S88_DATAIN_PIN 7#define S88_CLOCK_PIN 2#define S88_PS_PIN 3#define S88_CLOCK_INTERRUPT 0#define S88_PS_INTERRUPT 1const int nrOfOccDetectors = 6 * 16;volatile byte pendingStates[nrOfOccDetectors];volatile byte dataRegister[nrOfOccDetectors];volatile long clockTicks = 0;CanBus *canBus;
void PS_Int(){ // // Load the detector states in the data register // memcpy(dataRegister,pendingStates,nrOfOccDetectors); clockTicks = 0; // reset index in the dataRegister}//// Set the state of the next detector on the output pin//void clock_Int(){ digitalWrite(S88_DATAOUT_PIN, dataRegister[clockTicks]); // // If we are chained to additional detectors, reads a bit from the chain and // store it in the current, just written, position // // // Delay makes reading output signal from next Arduino in chain more reliable. // //delayMicroseconds(2); //dataRegister[clockTicks]=digitalRead(S88_DATAIN_PIN); clockTicks = (++clockTicks) % nrOfOccDetectors; // Goto next bit in dataRegister}
//// A function that executes whenever a message is received from the Canbus// It's purpose is to analyze the received event Occupancy detector and// update the proper element in the semaphore table or turnout table.//void occDtReceiver(unsigned char aMsgLen, OCC_DT_MSG *msgData){ if (msgData->address < nrOfOccDetectors) { pendingStates[msgData->address] = msgData->state; }}
void setup(){ // // Intialize the dataRegister // memset(pendingStates,0,nrOfOccDetectors); memset(dataRegister,0,nrOfOccDetectors); // // Setup the input/output pins and the interrupts for the S88 communication // //pinMode(S88_CLOCK_PIN, INPUT_PULLUP); //pinMode(S88_PS_PIN, INPUT_PULLUP); pinMode(S88_DATAIN_PIN, INPUT_PULLUP); pinMode(S88_DATAOUT_PIN, OUTPUT); digitalWrite(S88_DATAOUT_PIN, LOW); attachInterrupt(S88_CLOCK_INTERRUPT, clock_Int, RISING); attachInterrupt(S88_PS_INTERRUPT, PS_Int, RISING); // // Setup and start the CanBus communication // canBus = new CanBus(S88_INTERFACE_ID, nullptr, new Mcp2515(10)); // Only a receiver canBus->setMsgReceiver(OCC_DT, occDtReceiver); canBus->begin();}
void loop(){ // // Are there messages on the CanBus? // canBus->heartBeat();}
Code: [Selecteer]CanBus *canBus;
CanBus *canBus;
//// Type definition for the call back routine which will handle a state change//typedef void (*stateHandler_t)(byte s88Bank, byte s88Id, bool occupiedState);
class Detector{protected: short pin; // Digital pin in use by Detector int occupiedValue; // Value of the digitalRead() when the detector // is occupied int occupiedPinMode; // value set by pinMode() during attach bool occupied; // true is occupied, false is free char *description; // Just a description, used for debugging byte s88Bank; // The S88 bank (containg 16 detectors) byte s88Id; // The number of the detector in the S88 bank // // A number of timer values (in mS) used while switching from // free to occupied or switching from occupied to free // short waitTimeOn; short waitTimeOff; long pendingTime; bool pendingState; long timeOfLastMessageSend; // Last time the state handler was invoked stateHandler_t stateHandler; // An external interrupt routine that handles // a state switch of the detector // // Default functionality, common for most of the detector classes. // // // Initial setup of the class. // void init(char *aDescription, short aPin, byte aS88Bank, byte aS88Id); // // The basic standard heartbeat method, reponsible for monitoring the // detector and invoking the state handler when a state changed. // void doHeartBeat(); // // The basic attach functionality, responsible for setting up the // digital pin in use by a detector. // void doAttach(); public: // // Register the external state handler, respomsible for the proper handling // of a detector state change. // void registerStateHandler(stateHandler_t aStateHandler); virtual void heartBeat() = 0; virtual void attach() = 0;};
//// A Class that handles the MERG DTC8/DTC2 detector. See www.merg.org.uk//// it uses the following settings:// PinMode INPUT// pinValue for occupancy LOW// waitTimeOn 0// waitTimeOff 0//class DTC8 : public Detector{public: DTC8(char *aDescription, short aPin, byte aS88Bank, byte aS88Id); void heartBeat(); void attach();};//// A Class that handles the MERG PMP7 (Piocket money project) detector. // See www.merg.org.uk//// it uses the following settings:// PinMode INPUT_PULLUP// pinValue for occupancy LOW// waitTimeOn 15mS to counteract bounce behavior// waitTimeOff 15ms to counteract bounce behavior//class PMP7 : public Detector{public: PMP7(char *aDescription, short aPin, byte aS88Bank, byte aS88Id); void heartBeat(); void attach();};//// A Class that handles a modified Arduino infrared sensor detector. //// it uses the following settings:// PinMode INPUT_PULLUP// pinValue for occupancy HIGH// waitTimeOn 5mS // waitTimeOff 2000ms to counteract short interrupts caused by the wagons//class LS : public Detector{public: LS(char *aDescription, short aPin, byte aS88Bank, byte aS88Id); void heartBeat(); void attach();};