Facebook
From Marc, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 125
  1. /**
  2.  * A Legoino example to connect two train hubs and one powered up remote.
  3.  *
  4.  * 1) Power up the ESP32
  5.  * 2) Power up the Remote and the Train Hubs in any order
  6.  *
  7.  * You can change the motor speed of train 1 with the left (A) remote buttons
  8.  * You can change the motor speed of train 2 with the right (B) remote buttons
  9.  *
  10.  * (c) Copyright 2020
  11.  * Released under MIT License
  12.  *
  13.  */
  14.  
  15. #include "Lpf2Hub.h"
  16.  
  17. // create a hub instance
  18. Lpf2Hub myRemote;
  19. Lpf2Hub myTrainHub1;
  20.  
  21.  
  22. byte portLeft = (byte)PoweredUpRemoteHubPort::LEFT;
  23. byte portA = (byte)PoweredUpHubPort::A;
  24.  
  25. int currentSpeedTrain1 = 0;
  26. int updatedSpeedTrain1 = 0;
  27.  
  28. // additional rail rules
  29.  
  30. int speedLimit = 50;
  31. int newSpeed;
  32. bool stationBusy;
  33.  
  34.  
  35. bool isInitialized = false;
  36.  
  37. // callback function to handle updates of remote buttons
  38. void remoteCallback(void *hub, byte portNumber, DeviceType deviceType, uint8_t *pData)
  39. {
  40.   Lpf2Hub *myRemoteHub = (Lpf2Hub *)hub;
  41.  
  42. //  Serial.print("sensorMessage callback for port: ");
  43. //  Serial.println(portNumber, DEC);
  44.   if (deviceType == DeviceType::REMOTE_CONTROL_BUTTON)
  45.   {
  46.     ButtonState buttonState = myRemoteHub->parseRemoteButton(pData);
  47. //    Serial.print("Buttonstate: ");
  48. //    Serial.println((byte)buttonState, HEX);
  49.  
  50.     // Do the logic for left buttons of remote control and Train Hub 1
  51.     if (portNumber == (byte)portLeft && buttonState == ButtonState::UP)
  52.     {
  53.       updatedSpeedTrain1 = min(100, currentSpeedTrain1 + 10);
  54.     }
  55.     else if (portNumber == (byte)portLeft && buttonState == ButtonState::DOWN)
  56.     {
  57.       updatedSpeedTrain1 = min(100, currentSpeedTrain1 - 10);
  58.     }
  59.     else if (portNumber == (byte)portLeft && buttonState == ButtonState::STOP)
  60.     {
  61.       updatedSpeedTrain1 = 0;
  62.     }
  63.     //Only allow train to update speed if less then speed limit.
  64.     if (currentSpeedTrain1 != updatedSpeedTrain1 && updatedSpeedTrain1 <= speedLimit)
  65.     {
  66.         myTrainHub1.setBasicMotorSpeed(portA, updatedSpeedTrain1);
  67.         currentSpeedTrain1 = updatedSpeedTrain1;
  68.         Serial.print("Current speed :");
  69.         Serial.println(currentSpeedTrain1, DEC);
  70.     }
  71.     // Prints warning to user that command didnt run becuase it was over the speedLimit.
  72.     else if (updatedSpeedTrain1 > speedLimit)
  73.     {
  74.       Serial.print("Cant go faster then speed limit which is " + speedLimit);  
  75.     }
  76.  
  77.    
  78.   }
  79. }
  80.  
  81. // Rules of the railway function // In a function cuase this logic will get alot bigger
  82. // Function not being called at the moment for debugging purpose. // Function is called at top of void loop();
  83. // At the moment the function constantly checks no trains are going faster then the speed limit and if they are... slows them down.
  84. void rules(int current, int limit)
  85. {
  86.   //Checks current speed isnt higher then limit
  87.   if (current > limit)
  88.   {
  89.     newSpeed = limit;
  90.     myTrainHub1.setBasicMotorSpeed(portA, newSpeed);
  91.     Serial.print("To Fast - Speed Limit: " + limit); // This line is getting partly called for some reason when hitting the speeLimit logic in the remote callback function.
  92.     currentSpeedTrain1 = newSpeed;
  93.   }
  94. }
  95.  
  96. void setup()
  97. {
  98.   Serial.begin(115200);
  99.   myRemote.init();                       // initialize the remote control hub
  100.   myTrainHub1.init("90:84:2b:24:47:82"); // initialize the listening train hub 1 // here you have to use your own device ids
  101. }
  102.  
  103. // main loop
  104. void loop()
  105. {
  106.   //Checks rules of track
  107.   //rules(currentSpeedTrain1, speedLimit);
  108.  
  109.   //wait for three elements
  110.  
  111.   if (myRemote.isConnecting())
  112.   {
  113.     if (myRemote.getHubType() == HubType::POWERED_UP_REMOTE)
  114.     {
  115.       //This is the right device
  116.       if (!myRemote.connectHub())
  117.       {
  118.         Serial.println("Unable to connect to hub");
  119.       }
  120.       else
  121.       {
  122.         myRemote.setLedColor(GREEN);
  123.         Serial.println("Remote connected.");
  124.       }
  125.     }
  126.   }
  127.  
  128.   if (myTrainHub1.isConnecting())
  129.   {
  130.     if (myTrainHub1.getHubType() == HubType::POWERED_UP_HUB)
  131.     {
  132.       myTrainHub1.connectHub();
  133.       myTrainHub1.setLedColor(BLUE);
  134.       Serial.println("train hub 1 connected.");
  135.     }
  136.   }
  137.  
  138.   if (!myRemote.isConnected())
  139.   {
  140.     myRemote.init();
  141.   }
  142.  
  143.   if (!myTrainHub1.isConnected())
  144.   {
  145.     myTrainHub1.init();
  146.   }
  147.  
  148.   if (myRemote.isConnected() && myTrainHub1.isConnected() && !isInitialized)
  149.   {
  150.     Serial.println("System is initialized");
  151.     isInitialized = true;
  152.     delay(200); //needed because otherwise the message is to fast after the connection procedure and the message will get lost
  153.     // both activations are needed to get status updates
  154.     myRemote.activatePortDevice(portLeft, remoteCallback);
  155.     myRemote.setLedColor(WHITE);
  156.   }
  157. } // End of loop