Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*History Version 1.0 Ein & AUsgänge definiert 19.05.23 sowie CAN Nachrit senden
- Senden der Zustande der Eingänge Gleiseinfahrt 1-5 Via CAn an Zentrale Funktioniert
- 1.1 Daten direkt in CAN Data 0 schreiben Also ohne Variable Data00 für Nachricht 1
- 1.2 Leerzeilen gelöscht und CANmsg noch oben verschoben ungetestet
- 2.0 Empfangen von nachricht von Zentrale ausgänge Weiche Einfahrt SBH - funktioniert
- Sendeintervall auf 500mS gestellt
- 3.0 LSVmax hier und Weiche SBHSud einfahrt hinzugefügt
- Sendeinterval wegen LS Vmax auf 250mS gestellt
- 3.1 FahrstromEinfahrtSBHSud hinzugefügt
- 3.2 if schleife für zustand mit digitalWrite(variable, Variablestate) ersetzt
- 3.3 PWM Frequenz für Fahrstrom auf 100Hz geändert
- 4.0 Verschiedene Fahrspannungen eingepflegt
- 5.0 Bezeichnung zum Unterscheiden von Fahrspannung abschalten (Fahrstromxx) und Regelung via PWM (UFahrxxx) eingeführt
- Eingang überwachung Umfahrleis hinzugefügt Test auf Testbrett Ok
- 6.0 CAN Nachricht in Funktion gepackt und nur bei Zustandswechsel senden und Funktion für auslesen dre Eingänge programiert
- */
- // -------------------------------------- Umbenenen der STM Pins
- // Bliotheken
- //------------------------------------------------------------------------------------------------------------Testweise auskommentiert #include <assert.h>
- #include "stm32f103.h" // Bei selbst erstellten Bibliotheken der name immer in Anführungszeichen!! Nicht in <> kleiner als Zeichen!
- // EIngaenge
- // Lichtschranke
- const int LSEinfahrtSBHSudGleis1 = PB12;
- const int LSEinfahrtSBHSudGleis2 = PB13;
- const int LSEinfahrtSBHSudGleis3 = PB14;
- const int LSEinfahrtSBHSudGleis4 = PB15;
- const int LSEinfahrtSBHSudGleis5 = PA8;
- const int SensorFahrstrom = PB1;
- const int LSUmfahrSBHSud = PA9;
- uint8_t LSEinfahrtSBHSudGleis1State = 0;
- uint8_t LSEinfahrtSBHSudGleis2State = 0;
- uint8_t LSEinfahrtSBHSudGleis3State = 0;
- uint8_t LSEinfahrtSBHSudGleis4State = 0;
- uint8_t LSEinfahrtSBHSudGleis5State = 0;
- uint8_t SensorFahrstromState = 0;
- uint8_t LSUmfahrSBHSudState = 0;
- // Ausgaenge
- const int WeicheSBHSudGleis1Geradeaus = PA0;
- const int WeicheSBHSudGleis1Abbiegen = PA1;
- const int WeicheSBHSudGleis2Geradeaus = PA2;
- const int WeicheSBHSudGleis2Abbiegen = PA3;
- const int WeicheSBHSudGleis3Geradeaus = PA4;
- const int WeicheSBHSudGleis3Abbiegen = PA5;
- const int WeicheSBHSudGleis4Geradeaus = PA7;
- const int WeicheSBHSudGleis4Abbiegen = PB10;
- const int FahrstromModulNotHalt = PB7;
- const int UFahrSBHSudEinfahrt = PB0; // Regelung PWM
- const int UFahrKreuzungWendelSud = PA6; // Regelung PWM
- const int FahrstromEinfahrtSBHSud = PB11;
- const int FahrstromUmfahrSBHSud = PA10;
- const int FahrstromAusfahrtSBHSud = PB5;
- uint8_t WeicheSBHSudGleis1GeradeausState = 0;
- uint8_t WeicheSBHSudGleis1AbbiegenState = 0;
- uint8_t WeicheSBHSudGleis2GeradeausState = 0;
- uint8_t WeicheSBHSudGleis2AbbiegenState = 0;
- uint8_t WeicheSBHSudGleis3GeradeausState = 0;
- uint8_t WeicheSBHSudGleis3AbbiegenState = 0;
- uint8_t WeicheSBHSudGleis4GeradeausState = 0;
- uint8_t WeicheSBHSudGleis4AbbiegenState = 0;
- uint8_t FahrstromModulNotHaltState = 0;
- uint8_t UFahrSBHSudEinfahrtState = 0;
- uint8_t UFahrKreuzungWendelSudState = 0;
- uint8_t FahrstromEinfahrtSBHSudState = 0;
- uint8_t FahrstromUmfahrSBHSudState = 0;
- uint8_t FahrstromAusfahrtSBHSudState = 0;
- // Variablen
- // Test
- uint8_t M3G1 = 111;
- uint8_t M3G2 = 222;
- uint8_t M3G3 = 333;
- uint8_t M3G4 = 444;
- uint8_t M3G5 = 555;
- bool M3G1Gezahlt = 0;
- bool M3G2Gezahlt = 0;
- bool M3G3Gezahlt = 0;
- bool M3G4Gezahlt = 0;
- bool M3G5Gezahlt = 0;
- // CAN
- uint8_t counter = 0; // Zahler für nachricht wurde x mal versand
- uint8_t frameLength = 0; // framelegth ist die länge der nachricht hier kann man auch im Sendeteil eine Zahl angeben
- unsigned long previousMillis = 0; // stores last time output was updated
- const long interval = 50; // transmission interval (milliseconds) Intervall in dem gesendet wird
- CAN_msg_t CAN_TX_msg; // Initialisierung Senden
- CAN_msg_t CAN_RX_msg; // Initialisierung EMPFANGEN
- // Sonstige Variablen
- bool FahrstromFehlerMODUL3EINF = 0;
- // ------------------------------------ Funktionen
- void CANMSG0x100002()
- {
- // Daten zur Übertragung bereit stellen
- AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
- FahrstromFehlerMODUL3EINF = FahrstromModulNotHaltState ;
- // ---------------------------- CAN Nachricht 0x10002 senden
- // eigentliche Daten werden eingefüllt
- bitWrite (CAN_TX_msg.data[0], 0, LSEinfahrtSBHSudGleis1State); // mit bitwrite lassen sich einzelne bits innerhalb eines Bytes beschreiben. Hier Zustand TasterState in Erstes (ganz rechtes) Bit von Data0 schreiben
- bitWrite (CAN_TX_msg.data[0], 1, LSEinfahrtSBHSudGleis2State);
- bitWrite (CAN_TX_msg.data[0], 2, LSEinfahrtSBHSudGleis3State);
- bitWrite (CAN_TX_msg.data[0], 3, LSEinfahrtSBHSudGleis4State);
- bitWrite (CAN_TX_msg.data[0], 4, LSEinfahrtSBHSudGleis5State);
- bitWrite (CAN_TX_msg.data[0], 5, FahrstromFehlerMODUL3EINF);
- bitWrite (CAN_TX_msg.data[0], 6, LSUmfahrSBHSudState);
- CAN_TX_msg.data[1] = M3G1;
- CAN_TX_msg.data[2] = M3G2;
- CAN_TX_msg.data[3] = M3G3;
- CAN_TX_msg.data[4] = M3G4;
- CAN_TX_msg.data[5] = M3G5;
- CAN_TX_msg.len = 6; // ---- Nachrichtenlänge 1
- unsigned long currentMillis = millis(); // Senden alle x millisekunden
- if (currentMillis - previousMillis >= interval)
- {
- previousMillis = currentMillis;
- if ( ( counter % 2) == 0)
- { // Rest der Nachricht wird befüllt
- CAN_TX_msg.type = DATA_FRAME;
- if (CAN_TX_msg.len == 0) CAN_TX_msg.type = REMOTE_FRAME;
- CAN_TX_msg.format = EXTENDED_FORMAT;
- CAN_TX_msg.id = 0x100002; // nachrichten ID
- }
- CANSend(&CAN_TX_msg); // Übergabe der Nachricht an die Hardware
- }
- }
- void AUSLESNSENSOREN()
- {
- // ---------------------------- Alle relevanten Eingänge abfragen
- LSEinfahrtSBHSudGleis1State = digitalRead(LSEinfahrtSBHSudGleis1);
- LSEinfahrtSBHSudGleis2State = digitalRead(LSEinfahrtSBHSudGleis2);
- LSEinfahrtSBHSudGleis3State = digitalRead(LSEinfahrtSBHSudGleis3);
- LSEinfahrtSBHSudGleis4State = digitalRead(LSEinfahrtSBHSudGleis4);
- LSEinfahrtSBHSudGleis5State = digitalRead(LSEinfahrtSBHSudGleis5);
- LSUmfahrSBHSudState = digitalRead(LSUmfahrSBHSud);
- }
- // -------------------------- State Maschine
- enum MODUL3EINF
- {
- BEREIT, SENDEN1, WARTEN, SENDEN2,
- };
- MODUL3EINF akt_state_MODUL3EINF = BEREIT;
- MODUL3EINF next_state_MODUL3EINF = BEREIT;
- enum MODUL3UMFAHR
- {
- BEREITUMF, SENDEN1UMF, WARTENUMF, SENDEN2UMF,
- };
- MODUL3UMFAHR akt_state_MODUL3UMFAHR = BEREITUMF;
- MODUL3UMFAHR next_state_MODUL3UMFAHR = BEREITUMF;
- void setup()
- {
- WeicheSBHSudGleis1GeradeausState = HIGH;
- pinMode(LED_BUILTIN, OUTPUT); //LED auf Arduino Blinken um programierung zu bestätigen
- delay(1500);
- digitalWrite(PC13, HIGH);
- delay(500);
- digitalWrite(PC13, LOW);
- delay(500);
- digitalWrite(PC13, HIGH);
- delay(500);
- digitalWrite(PC13, LOW);
- delay(500);
- digitalWrite(PC13, HIGH);
- // CAN aktivieren
- {
- Serial.begin(115200);
- bool ret = CANInit(CAN_500KBPS, 0); // CAN_RX mapped to PA11, CAN_TX mapped to PA12
- if (!ret) while (true);
- }
- // -------------------------------------- pinMode
- pinMode(LSEinfahrtSBHSudGleis1, INPUT);
- pinMode(LSEinfahrtSBHSudGleis2, INPUT);
- pinMode(LSEinfahrtSBHSudGleis3, INPUT);
- pinMode(LSEinfahrtSBHSudGleis4, INPUT);
- pinMode(LSEinfahrtSBHSudGleis5, INPUT);
- pinMode(SensorFahrstrom, INPUT);
- pinMode(WeicheSBHSudGleis1Geradeaus, OUTPUT);
- pinMode(WeicheSBHSudGleis1Abbiegen, OUTPUT);
- pinMode(WeicheSBHSudGleis2Geradeaus, OUTPUT);
- pinMode(WeicheSBHSudGleis2Abbiegen, OUTPUT);
- pinMode(WeicheSBHSudGleis3Geradeaus, OUTPUT);
- pinMode(WeicheSBHSudGleis3Abbiegen, OUTPUT);
- pinMode(WeicheSBHSudGleis4Geradeaus, OUTPUT);
- pinMode(WeicheSBHSudGleis4Abbiegen, OUTPUT);
- pinMode(FahrstromModulNotHalt, OUTPUT);
- pinMode(UFahrSBHSudEinfahrt, OUTPUT);
- pinMode(UFahrKreuzungWendelSud, OUTPUT);
- pinMode(FahrstromEinfahrtSBHSud, OUTPUT);
- pinMode(FahrstromUmfahrSBHSud, OUTPUT);
- pinMode(FahrstromAusfahrtSBHSud, OUTPUT);
- }
- void loop()
- {
- // ---------------------------- Begin CAN Empfangen
- {
- if (CANMsgAvail())
- {
- CANReceive(&CAN_RX_msg); // Prüfen ob neue Nachricht empfangen werden kann (liegt eine neue Nachricht zum abholen bereit?)
- if (CAN_RX_msg.type == DATA_FRAME)
- {
- if (CAN_RX_msg.id == 0x100010) // Empfang der nachricht 0x100010 von von Zentrale
- {
- WeicheSBHSudGleis1GeradeausState = bitRead (CAN_RX_msg.data[0], 0);
- WeicheSBHSudGleis1AbbiegenState = bitRead (CAN_RX_msg.data[0], 1);
- WeicheSBHSudGleis2GeradeausState = bitRead (CAN_RX_msg.data[0], 2);
- WeicheSBHSudGleis2AbbiegenState = bitRead (CAN_RX_msg.data[0], 3);
- WeicheSBHSudGleis3GeradeausState = bitRead (CAN_RX_msg.data[0], 4);
- WeicheSBHSudGleis3AbbiegenState = bitRead (CAN_RX_msg.data[0], 5);
- WeicheSBHSudGleis4GeradeausState = bitRead (CAN_RX_msg.data[0], 6);
- WeicheSBHSudGleis4AbbiegenState = bitRead (CAN_RX_msg.data[0], 7);
- FahrstromEinfahrtSBHSudState = bitRead (CAN_RX_msg.data[1], 0);
- FahrstromAusfahrtSBHSudState = bitRead (CAN_RX_msg.data[1], 1);
- FahrstromUmfahrSBHSudState = bitRead (CAN_RX_msg.data[1], 2);
- UFahrSBHSudEinfahrtState = (CAN_RX_msg.data[2]);
- UFahrKreuzungWendelSudState = (CAN_RX_msg.data[3]);
- }
- }
- }
- }
- {
- // ---------------------------- State maschine
- if (next_state_MODUL3EINF != akt_state_MODUL3EINF)
- {
- switch (next_state_MODUL3EINF)
- {
- case BEREIT:
- {
- break;
- }
- case SENDEN1:
- {
- break;
- }
- case WARTEN:
- {
- break;
- }
- case SENDEN2:
- {
- break;
- }
- }
- akt_state_MODUL3EINF = next_state_MODUL3EINF; // Zustand in aktstate schreiben
- }
- switch (akt_state_MODUL3EINF)
- {
- case BEREIT:
- {
- AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
- // Wenn eine der 5 Lichtschranken durchbrochen wird
- if ( LSEinfahrtSBHSudGleis1State == HIGH || LSEinfahrtSBHSudGleis2State == HIGH || LSEinfahrtSBHSudGleis3State == HIGH || LSEinfahrtSBHSudGleis4State == HIGH || LSEinfahrtSBHSudGleis5State == HIGH)
- {
- next_state_MODUL3EINF = SENDEN1;
- }
- break;
- }
- case SENDEN1:
- {
- CANMSG0x100002();
- next_state_MODUL3EINF = WARTEN;
- break;
- }
- case WARTEN:
- {
- AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
- // Wenn alle Lichtschranken wieder LOW sind:
- if ( LSEinfahrtSBHSudGleis1State == LOW && LSEinfahrtSBHSudGleis2State == LOW && LSEinfahrtSBHSudGleis3State == LOW && LSEinfahrtSBHSudGleis4State == LOW && LSEinfahrtSBHSudGleis5State == LOW)
- {
- next_state_MODUL3EINF = SENDEN2;
- }
- break;
- }
- case SENDEN2:
- {
- CANMSG0x100002();
- next_state_MODUL3EINF = BEREIT;
- break;
- }
- }
- }
- {
- SensorFahrstromState = analogRead(SensorFahrstrom);
- if (SensorFahrstromState > 200)
- {
- FahrstromModulNotHaltState = 1;
- }
- else
- {
- FahrstromModulNotHaltState = 1;
- }
- FahrstromFehlerMODUL3EINF = FahrstromModulNotHaltState ;
- }
- //------ Schalten der Ausgaenge
- {
- analogWriteFrequency(100);
- analogWrite(UFahrSBHSudEinfahrt, UFahrSBHSudEinfahrtState);
- analogWrite(UFahrKreuzungWendelSud, UFahrKreuzungWendelSudState);
- digitalWrite(FahrstromEinfahrtSBHSud, FahrstromEinfahrtSBHSudState);
- digitalWrite(WeicheSBHSudGleis1Geradeaus, WeicheSBHSudGleis1GeradeausState);
- digitalWrite(WeicheSBHSudGleis1Abbiegen, WeicheSBHSudGleis1AbbiegenState);
- digitalWrite(WeicheSBHSudGleis2Geradeaus, WeicheSBHSudGleis2GeradeausState);
- digitalWrite(WeicheSBHSudGleis2Abbiegen, WeicheSBHSudGleis2AbbiegenState);
- digitalWrite(WeicheSBHSudGleis3Geradeaus, WeicheSBHSudGleis3GeradeausState);
- digitalWrite(WeicheSBHSudGleis3Abbiegen, WeicheSBHSudGleis3AbbiegenState);
- digitalWrite(WeicheSBHSudGleis4Geradeaus, WeicheSBHSudGleis4GeradeausState);
- digitalWrite(WeicheSBHSudGleis4Abbiegen, WeicheSBHSudGleis4AbbiegenState);
- digitalWrite(FahrstromUmfahrSBHSud, FahrstromUmfahrSBHSudState);
- digitalWrite(FahrstromAusfahrtSBHSud, FahrstromAusfahrtSBHSudState);
- digitalWrite(FahrstromModulNotHalt, FahrstromModulNotHaltState);
- }
- {
- // -------------- Tests
- if (LSEinfahrtSBHSudGleis1State == HIGH)
- {
- if (M3G1Gezahlt == 0)
- {
- M3G1 = M3G1 + 1;
- M3G1Gezahlt = 1;
- }
- }
- else
- {
- M3G1Gezahlt = 0;
- }
- if (LSEinfahrtSBHSudGleis2State == HIGH)
- {
- if (M3G2Gezahlt == 0)
- {
- M3G2 = M3G2 + 1;
- M3G2Gezahlt = 1;
- }
- }
- else
- {
- M3G2Gezahlt = 0;
- }
- if (LSEinfahrtSBHSudGleis3State == HIGH)
- {
- if (M3G3Gezahlt == 0)
- {
- M3G3 = M3G3 + 1;
- M3G3Gezahlt = 1;
- }
- }
- else
- {
- M3G3Gezahlt = 0;
- }
- if (LSEinfahrtSBHSudGleis4State == HIGH)
- {
- if (M3G4Gezahlt == 0)
- {
- M3G4 = M3G4 + 1;
- M3G4Gezahlt = 1;
- }
- }
- else
- {
- M3G4Gezahlt = 0;
- }
- if (LSEinfahrtSBHSudGleis5State == HIGH)
- {
- if (M3G5Gezahlt == 0)
- {
- M3G5 = M3G5 + 1;
- M3G5Gezahlt = 1;
- }
- }
- else
- {
- M3G5Gezahlt = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment