KGBRUS

statemaswchOK

Jan 6th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.61 KB | None | 0 0
  1. /*History Version 1.0 Ein & AUsgänge definiert 19.05.23 sowie CAN Nachrit senden
  2. Senden der Zustande der Eingänge Gleiseinfahrt 1-5 Via CAn an Zentrale Funktioniert
  3. 1.1 Daten direkt in CAN Data 0 schreiben Also ohne Variable Data00 für Nachricht 1
  4. 1.2 Leerzeilen gelöscht und CANmsg noch oben verschoben ungetestet
  5. 2.0 Empfangen von nachricht von Zentrale ausgänge Weiche Einfahrt SBH - funktioniert
  6. Sendeintervall auf 500mS gestellt
  7. 3.0 LSVmax hier und Weiche SBHSud einfahrt hinzugefügt
  8. Sendeinterval wegen LS Vmax auf 250mS gestellt
  9. 3.1 FahrstromEinfahrtSBHSud hinzugefügt
  10. 3.2 if schleife für zustand mit digitalWrite(variable, Variablestate) ersetzt
  11. 3.3 PWM Frequenz für Fahrstrom auf 100Hz geändert
  12. 4.0 Verschiedene Fahrspannungen eingepflegt
  13. 5.0 Bezeichnung zum Unterscheiden von Fahrspannung abschalten (Fahrstromxx) und Regelung via PWM (UFahrxxx) eingeführt
  14. Eingang überwachung Umfahrleis hinzugefügt Test auf Testbrett Ok
  15. 6.0 CAN Nachricht in Funktion gepackt und nur bei Zustandswechsel senden und Funktion für auslesen dre Eingänge programiert
  16.  
  17.  
  18. */
  19. // -------------------------------------- Umbenenen der STM Pins
  20. // Bliotheken
  21. //------------------------------------------------------------------------------------------------------------Testweise auskommentiert #include <assert.h>
  22. #include "stm32f103.h" // Bei selbst erstellten Bibliotheken der name immer in Anführungszeichen!! Nicht in <> kleiner als Zeichen!
  23.  
  24. // EIngaenge
  25. // Lichtschranke
  26. const int LSEinfahrtSBHSudGleis1 = PB12;
  27. const int LSEinfahrtSBHSudGleis2 = PB13;
  28. const int LSEinfahrtSBHSudGleis3 = PB14;
  29. const int LSEinfahrtSBHSudGleis4 = PB15;
  30. const int LSEinfahrtSBHSudGleis5 = PA8;
  31. const int SensorFahrstrom = PB1;
  32. const int LSUmfahrSBHSud = PA9;
  33.  
  34. uint8_t LSEinfahrtSBHSudGleis1State = 0;
  35. uint8_t LSEinfahrtSBHSudGleis2State = 0;
  36. uint8_t LSEinfahrtSBHSudGleis3State = 0;
  37. uint8_t LSEinfahrtSBHSudGleis4State = 0;
  38. uint8_t LSEinfahrtSBHSudGleis5State = 0;
  39. uint8_t SensorFahrstromState = 0;
  40. uint8_t LSUmfahrSBHSudState = 0;
  41.  
  42.  
  43.  
  44. // Ausgaenge
  45.  
  46. const int WeicheSBHSudGleis1Geradeaus = PA0;
  47. const int WeicheSBHSudGleis1Abbiegen = PA1;
  48. const int WeicheSBHSudGleis2Geradeaus = PA2;
  49. const int WeicheSBHSudGleis2Abbiegen = PA3;
  50. const int WeicheSBHSudGleis3Geradeaus = PA4;
  51. const int WeicheSBHSudGleis3Abbiegen = PA5;
  52. const int WeicheSBHSudGleis4Geradeaus = PA7;
  53. const int WeicheSBHSudGleis4Abbiegen = PB10;
  54. const int FahrstromModulNotHalt = PB7;
  55. const int UFahrSBHSudEinfahrt = PB0; // Regelung PWM
  56. const int UFahrKreuzungWendelSud = PA6; // Regelung PWM
  57. const int FahrstromEinfahrtSBHSud = PB11;
  58. const int FahrstromUmfahrSBHSud = PA10;
  59. const int FahrstromAusfahrtSBHSud = PB5;
  60.  
  61.  
  62.  
  63. uint8_t WeicheSBHSudGleis1GeradeausState = 0;
  64. uint8_t WeicheSBHSudGleis1AbbiegenState = 0;
  65. uint8_t WeicheSBHSudGleis2GeradeausState = 0;
  66. uint8_t WeicheSBHSudGleis2AbbiegenState = 0;
  67. uint8_t WeicheSBHSudGleis3GeradeausState = 0;
  68. uint8_t WeicheSBHSudGleis3AbbiegenState = 0;
  69. uint8_t WeicheSBHSudGleis4GeradeausState = 0;
  70. uint8_t WeicheSBHSudGleis4AbbiegenState = 0;
  71. uint8_t FahrstromModulNotHaltState = 0;
  72. uint8_t UFahrSBHSudEinfahrtState = 0;
  73. uint8_t UFahrKreuzungWendelSudState = 0;
  74. uint8_t FahrstromEinfahrtSBHSudState = 0;
  75. uint8_t FahrstromUmfahrSBHSudState = 0;
  76. uint8_t FahrstromAusfahrtSBHSudState = 0;
  77.  
  78.  
  79. // Variablen
  80.  
  81. // Test
  82. uint8_t M3G1 = 111;
  83. uint8_t M3G2 = 222;
  84. uint8_t M3G3 = 333;
  85. uint8_t M3G4 = 444;
  86. uint8_t M3G5 = 555;
  87. bool M3G1Gezahlt = 0;
  88. bool M3G2Gezahlt = 0;
  89. bool M3G3Gezahlt = 0;
  90. bool M3G4Gezahlt = 0;
  91. bool M3G5Gezahlt = 0;
  92. // CAN
  93. uint8_t counter = 0; // Zahler für nachricht wurde x mal versand
  94. uint8_t frameLength = 0; // framelegth ist die länge der nachricht hier kann man auch im Sendeteil eine Zahl angeben
  95. unsigned long previousMillis = 0; // stores last time output was updated
  96. const long interval = 50; // transmission interval (milliseconds) Intervall in dem gesendet wird
  97. CAN_msg_t CAN_TX_msg; // Initialisierung Senden
  98. CAN_msg_t CAN_RX_msg; // Initialisierung EMPFANGEN
  99.  
  100. // Sonstige Variablen
  101.  
  102. bool FahrstromFehlerMODUL3EINF = 0;
  103.  
  104. // ------------------------------------ Funktionen
  105. void CANMSG0x100002()
  106. {
  107. // Daten zur Übertragung bereit stellen
  108. AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
  109. FahrstromFehlerMODUL3EINF = FahrstromModulNotHaltState ;
  110. // ---------------------------- CAN Nachricht 0x10002 senden
  111. // eigentliche Daten werden eingefüllt
  112. 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
  113. bitWrite (CAN_TX_msg.data[0], 1, LSEinfahrtSBHSudGleis2State);
  114. bitWrite (CAN_TX_msg.data[0], 2, LSEinfahrtSBHSudGleis3State);
  115. bitWrite (CAN_TX_msg.data[0], 3, LSEinfahrtSBHSudGleis4State);
  116. bitWrite (CAN_TX_msg.data[0], 4, LSEinfahrtSBHSudGleis5State);
  117. bitWrite (CAN_TX_msg.data[0], 5, FahrstromFehlerMODUL3EINF);
  118. bitWrite (CAN_TX_msg.data[0], 6, LSUmfahrSBHSudState);
  119. CAN_TX_msg.data[1] = M3G1;
  120. CAN_TX_msg.data[2] = M3G2;
  121. CAN_TX_msg.data[3] = M3G3;
  122. CAN_TX_msg.data[4] = M3G4;
  123. CAN_TX_msg.data[5] = M3G5;
  124. CAN_TX_msg.len = 6; // ---- Nachrichtenlänge 1
  125. unsigned long currentMillis = millis(); // Senden alle x millisekunden
  126. if (currentMillis - previousMillis >= interval)
  127. {
  128. previousMillis = currentMillis;
  129. if ( ( counter % 2) == 0)
  130. { // Rest der Nachricht wird befüllt
  131. CAN_TX_msg.type = DATA_FRAME;
  132. if (CAN_TX_msg.len == 0) CAN_TX_msg.type = REMOTE_FRAME;
  133. CAN_TX_msg.format = EXTENDED_FORMAT;
  134. CAN_TX_msg.id = 0x100002; // nachrichten ID
  135. }
  136. CANSend(&CAN_TX_msg); // Übergabe der Nachricht an die Hardware
  137. }
  138. }
  139. void AUSLESNSENSOREN()
  140. {
  141. // ---------------------------- Alle relevanten Eingänge abfragen
  142. LSEinfahrtSBHSudGleis1State = digitalRead(LSEinfahrtSBHSudGleis1);
  143. LSEinfahrtSBHSudGleis2State = digitalRead(LSEinfahrtSBHSudGleis2);
  144. LSEinfahrtSBHSudGleis3State = digitalRead(LSEinfahrtSBHSudGleis3);
  145. LSEinfahrtSBHSudGleis4State = digitalRead(LSEinfahrtSBHSudGleis4);
  146. LSEinfahrtSBHSudGleis5State = digitalRead(LSEinfahrtSBHSudGleis5);
  147. LSUmfahrSBHSudState = digitalRead(LSUmfahrSBHSud);
  148. }
  149.  
  150. // -------------------------- State Maschine
  151.  
  152. enum MODUL3EINF
  153. {
  154. BEREIT, SENDEN1, WARTEN, SENDEN2,
  155. };
  156. MODUL3EINF akt_state_MODUL3EINF = BEREIT;
  157. MODUL3EINF next_state_MODUL3EINF = BEREIT;
  158. enum MODUL3UMFAHR
  159. {
  160. BEREITUMF, SENDEN1UMF, WARTENUMF, SENDEN2UMF,
  161. };
  162. MODUL3UMFAHR akt_state_MODUL3UMFAHR = BEREITUMF;
  163. MODUL3UMFAHR next_state_MODUL3UMFAHR = BEREITUMF;
  164.  
  165. void setup()
  166. {
  167. WeicheSBHSudGleis1GeradeausState = HIGH;
  168. pinMode(LED_BUILTIN, OUTPUT); //LED auf Arduino Blinken um programierung zu bestätigen
  169. delay(1500);
  170. digitalWrite(PC13, HIGH);
  171. delay(500);
  172. digitalWrite(PC13, LOW);
  173. delay(500);
  174. digitalWrite(PC13, HIGH);
  175. delay(500);
  176. digitalWrite(PC13, LOW);
  177. delay(500);
  178. digitalWrite(PC13, HIGH);
  179.  
  180. // CAN aktivieren
  181. {
  182. Serial.begin(115200);
  183. bool ret = CANInit(CAN_500KBPS, 0); // CAN_RX mapped to PA11, CAN_TX mapped to PA12
  184. if (!ret) while (true);
  185. }
  186. // -------------------------------------- pinMode
  187. pinMode(LSEinfahrtSBHSudGleis1, INPUT);
  188. pinMode(LSEinfahrtSBHSudGleis2, INPUT);
  189. pinMode(LSEinfahrtSBHSudGleis3, INPUT);
  190. pinMode(LSEinfahrtSBHSudGleis4, INPUT);
  191. pinMode(LSEinfahrtSBHSudGleis5, INPUT);
  192. pinMode(SensorFahrstrom, INPUT);
  193.  
  194.  
  195. pinMode(WeicheSBHSudGleis1Geradeaus, OUTPUT);
  196. pinMode(WeicheSBHSudGleis1Abbiegen, OUTPUT);
  197. pinMode(WeicheSBHSudGleis2Geradeaus, OUTPUT);
  198. pinMode(WeicheSBHSudGleis2Abbiegen, OUTPUT);
  199. pinMode(WeicheSBHSudGleis3Geradeaus, OUTPUT);
  200. pinMode(WeicheSBHSudGleis3Abbiegen, OUTPUT);
  201. pinMode(WeicheSBHSudGleis4Geradeaus, OUTPUT);
  202. pinMode(WeicheSBHSudGleis4Abbiegen, OUTPUT);
  203. pinMode(FahrstromModulNotHalt, OUTPUT);
  204. pinMode(UFahrSBHSudEinfahrt, OUTPUT);
  205. pinMode(UFahrKreuzungWendelSud, OUTPUT);
  206. pinMode(FahrstromEinfahrtSBHSud, OUTPUT);
  207. pinMode(FahrstromUmfahrSBHSud, OUTPUT);
  208. pinMode(FahrstromAusfahrtSBHSud, OUTPUT);
  209. }
  210.  
  211. void loop()
  212. {
  213. // ---------------------------- Begin CAN Empfangen
  214. {
  215. if (CANMsgAvail())
  216. {
  217. CANReceive(&CAN_RX_msg); // Prüfen ob neue Nachricht empfangen werden kann (liegt eine neue Nachricht zum abholen bereit?)
  218. if (CAN_RX_msg.type == DATA_FRAME)
  219. {
  220. if (CAN_RX_msg.id == 0x100010) // Empfang der nachricht 0x100010 von von Zentrale
  221. {
  222. WeicheSBHSudGleis1GeradeausState = bitRead (CAN_RX_msg.data[0], 0);
  223. WeicheSBHSudGleis1AbbiegenState = bitRead (CAN_RX_msg.data[0], 1);
  224. WeicheSBHSudGleis2GeradeausState = bitRead (CAN_RX_msg.data[0], 2);
  225. WeicheSBHSudGleis2AbbiegenState = bitRead (CAN_RX_msg.data[0], 3);
  226. WeicheSBHSudGleis3GeradeausState = bitRead (CAN_RX_msg.data[0], 4);
  227. WeicheSBHSudGleis3AbbiegenState = bitRead (CAN_RX_msg.data[0], 5);
  228. WeicheSBHSudGleis4GeradeausState = bitRead (CAN_RX_msg.data[0], 6);
  229. WeicheSBHSudGleis4AbbiegenState = bitRead (CAN_RX_msg.data[0], 7);
  230. FahrstromEinfahrtSBHSudState = bitRead (CAN_RX_msg.data[1], 0);
  231. FahrstromAusfahrtSBHSudState = bitRead (CAN_RX_msg.data[1], 1);
  232. FahrstromUmfahrSBHSudState = bitRead (CAN_RX_msg.data[1], 2);
  233.  
  234. UFahrSBHSudEinfahrtState = (CAN_RX_msg.data[2]);
  235. UFahrKreuzungWendelSudState = (CAN_RX_msg.data[3]);
  236. }
  237. }
  238.  
  239. }
  240. }
  241.  
  242.  
  243. {
  244. // ---------------------------- State maschine
  245. if (next_state_MODUL3EINF != akt_state_MODUL3EINF)
  246. {
  247. switch (next_state_MODUL3EINF)
  248. {
  249. case BEREIT:
  250. {
  251. break;
  252. }
  253. case SENDEN1:
  254. {
  255. break;
  256. }
  257. case WARTEN:
  258. {
  259. break;
  260. }
  261. case SENDEN2:
  262. {
  263. break;
  264. }
  265. }
  266. akt_state_MODUL3EINF = next_state_MODUL3EINF; // Zustand in aktstate schreiben
  267. }
  268. switch (akt_state_MODUL3EINF)
  269. {
  270. case BEREIT:
  271. {
  272.  
  273. AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
  274. // Wenn eine der 5 Lichtschranken durchbrochen wird
  275. if ( LSEinfahrtSBHSudGleis1State == HIGH || LSEinfahrtSBHSudGleis2State == HIGH || LSEinfahrtSBHSudGleis3State == HIGH || LSEinfahrtSBHSudGleis4State == HIGH || LSEinfahrtSBHSudGleis5State == HIGH)
  276. {
  277. next_state_MODUL3EINF = SENDEN1;
  278. }
  279. break;
  280. }
  281. case SENDEN1:
  282. {
  283. CANMSG0x100002();
  284. next_state_MODUL3EINF = WARTEN;
  285. break;
  286. }
  287. case WARTEN:
  288. {
  289. AUSLESNSENSOREN(); // Funktion Sensoren Auslesen aufrufen
  290. // Wenn alle Lichtschranken wieder LOW sind:
  291. if ( LSEinfahrtSBHSudGleis1State == LOW && LSEinfahrtSBHSudGleis2State == LOW && LSEinfahrtSBHSudGleis3State == LOW && LSEinfahrtSBHSudGleis4State == LOW && LSEinfahrtSBHSudGleis5State == LOW)
  292. {
  293. next_state_MODUL3EINF = SENDEN2;
  294. }
  295. break;
  296. }
  297. case SENDEN2:
  298. {
  299. CANMSG0x100002();
  300. next_state_MODUL3EINF = BEREIT;
  301. break;
  302. }
  303. }
  304.  
  305.  
  306.  
  307. }
  308. {
  309. SensorFahrstromState = analogRead(SensorFahrstrom);
  310. if (SensorFahrstromState > 200)
  311. {
  312. FahrstromModulNotHaltState = 1;
  313. }
  314. else
  315. {
  316. FahrstromModulNotHaltState = 1;
  317. }
  318. FahrstromFehlerMODUL3EINF = FahrstromModulNotHaltState ;
  319. }
  320. //------ Schalten der Ausgaenge
  321. {
  322.  
  323. analogWriteFrequency(100);
  324. analogWrite(UFahrSBHSudEinfahrt, UFahrSBHSudEinfahrtState);
  325. analogWrite(UFahrKreuzungWendelSud, UFahrKreuzungWendelSudState);
  326.  
  327. digitalWrite(FahrstromEinfahrtSBHSud, FahrstromEinfahrtSBHSudState);
  328. digitalWrite(WeicheSBHSudGleis1Geradeaus, WeicheSBHSudGleis1GeradeausState);
  329. digitalWrite(WeicheSBHSudGleis1Abbiegen, WeicheSBHSudGleis1AbbiegenState);
  330. digitalWrite(WeicheSBHSudGleis2Geradeaus, WeicheSBHSudGleis2GeradeausState);
  331. digitalWrite(WeicheSBHSudGleis2Abbiegen, WeicheSBHSudGleis2AbbiegenState);
  332. digitalWrite(WeicheSBHSudGleis3Geradeaus, WeicheSBHSudGleis3GeradeausState);
  333. digitalWrite(WeicheSBHSudGleis3Abbiegen, WeicheSBHSudGleis3AbbiegenState);
  334. digitalWrite(WeicheSBHSudGleis4Geradeaus, WeicheSBHSudGleis4GeradeausState);
  335. digitalWrite(WeicheSBHSudGleis4Abbiegen, WeicheSBHSudGleis4AbbiegenState);
  336. digitalWrite(FahrstromUmfahrSBHSud, FahrstromUmfahrSBHSudState);
  337. digitalWrite(FahrstromAusfahrtSBHSud, FahrstromAusfahrtSBHSudState);
  338. digitalWrite(FahrstromModulNotHalt, FahrstromModulNotHaltState);
  339.  
  340. }
  341. {
  342. // -------------- Tests
  343.  
  344. if (LSEinfahrtSBHSudGleis1State == HIGH)
  345. {
  346. if (M3G1Gezahlt == 0)
  347. {
  348. M3G1 = M3G1 + 1;
  349. M3G1Gezahlt = 1;
  350. }
  351. }
  352. else
  353. {
  354. M3G1Gezahlt = 0;
  355. }
  356.  
  357. if (LSEinfahrtSBHSudGleis2State == HIGH)
  358. {
  359. if (M3G2Gezahlt == 0)
  360. {
  361. M3G2 = M3G2 + 1;
  362. M3G2Gezahlt = 1;
  363. }
  364. }
  365. else
  366. {
  367. M3G2Gezahlt = 0;
  368. }
  369.  
  370. if (LSEinfahrtSBHSudGleis3State == HIGH)
  371. {
  372. if (M3G3Gezahlt == 0)
  373. {
  374. M3G3 = M3G3 + 1;
  375. M3G3Gezahlt = 1;
  376. }
  377. }
  378. else
  379. {
  380. M3G3Gezahlt = 0;
  381. }
  382. if (LSEinfahrtSBHSudGleis4State == HIGH)
  383. {
  384. if (M3G4Gezahlt == 0)
  385. {
  386. M3G4 = M3G4 + 1;
  387. M3G4Gezahlt = 1;
  388. }
  389. }
  390. else
  391. {
  392. M3G4Gezahlt = 0;
  393. }
  394.  
  395. if (LSEinfahrtSBHSudGleis5State == HIGH)
  396. {
  397. if (M3G5Gezahlt == 0)
  398. {
  399. M3G5 = M3G5 + 1;
  400. M3G5Gezahlt = 1;
  401. }
  402. }
  403. else
  404. {
  405. M3G5Gezahlt = 0;
  406. }
  407. }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment