Advertisement
SolarisFalls

Untitled

Jan 26th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include "BT_BaseTypes.h"
  2. #include "hardware/watchdog.h"
  3.  
  4. uint16_t CommsStatusBuffer[4] = { 0 };
  5.  
  6. teFUNC_STATUS OnTelemetryRequest(const tsCANTS_FRAME_TELEMETRY_REQUEST* TelemetryRequest)
  7. {
  8.   // Handle the response for the specific telemetry channel
  9.   switch (TelemetryRequest->iChannel)
  10.   {
  11.     case 0: /* Local Comms Temperature */
  12.       CANTS_SendTelemetryAcknowledgement(TelemetryRequest->iChannel, TelemetryRequest->iFromAddress, 0, 0, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  13.       break;
  14.  
  15.     case 1: /* Comms Status - Frequency, Received signal strength, Transmit power, Frame error */
  16.       CANTS_SendTelemetryAcknowledgement(TelemetryRequest->iChannel, TelemetryRequest->iFromAddress, (uint8_t*)CommsStatusBuffer, 8, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  17.      
  18.       break;
  19.  
  20.     default:
  21.       // Send negative acknowledgement because the channel isn't supported
  22.       CANTS_SendTelemetryAcknowledgement(TelemetryRequest->iChannel, TelemetryRequest->iFromAddress, 0, 0, CANTS_NEGATIVE_ACKNOWLEDGEMENT);
  23.       break;
  24.   }
  25.  
  26.   return BT_SUCCESS;
  27. }
  28.  
  29. teFUNC_STATUS OnTelecommandRequest(const tsCANTS_FRAME_TELECOMMAND_REQUEST* TelecommandRequest)
  30. {
  31.   // Forward definitions due to label jumps
  32.   uint8_t NewTXPower = CONFIG_RF69_DEFAULT_TRANSMIT_POWER_DBM;
  33.   uint8_t ColorR, ColorG, ColorB;
  34.   float NewFrequency;
  35.   uint8_t FrequencyIndex;
  36.  
  37.   if(TelecommandRequest->iToAddress == CONFIG_CANTS_NODE_ADDRESS)
  38.   {
  39.     switch (TelecommandRequest->iChannel)
  40.     {
  41.       case 0: /* Set frequency */
  42.         // Check the parameter is the correct length
  43.         if (TelecommandRequest->iDataLength != 1)
  44.         {
  45.           Serial.println("Set Frequency TC contains invalid parameter length");
  46.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  47.         }
  48.  
  49.         // Get the frequency index number
  50.         FrequencyIndex = TelecommandRequest->acData[0];
  51.  
  52.         // Ensure the frequency is within bounds
  53.         if (FrequencyIndex > 7)
  54.         {
  55.           Serial.println("Set Frequency TC contains invalid parameter length");
  56.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  57.         }
  58.  
  59.         // Assign the new frequency value
  60.         NewFrequency = EuropeFrequencies[FrequencyIndex];
  61.    
  62.         // Set the new frequency
  63.         if (!RF69Driver.setFrequency(NewFrequency))
  64.         {
  65.           Serial.println("Failed to set new frequency");
  66.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  67.         }
  68.  
  69.         // Update the global
  70.         RFFrequency = NewFrequency;
  71.  
  72.         // Send positive acknowledgement
  73.         CANTS_SendTelecommandAcknowledgement(TelecommandRequest->iChannel, TelecommandRequest->iFromAddress, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  74.        
  75.         break;
  76.      
  77.       case 1:/* Set TX power */
  78.         // Check the parameter is the correct length
  79.         if (TelecommandRequest->iDataLength != 1)
  80.         {
  81.           Serial.println("Set TX Power TC contains invalid parameter length");
  82.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  83.         }
  84.  
  85.         // Get the new TX power value
  86.         NewTXPower = TelecommandRequest->acData[0];
  87.  
  88.         // Ensure it does not exceed the maximum power
  89.         if (NewTXPower > CONFIG_RF69_MAXIMUM_TRANSMIT_POWER_DBM)
  90.         {
  91.           Serial.println("Set TX Power value too high");
  92.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  93.         }
  94.  
  95.         // Keep the global up to date
  96.         RFTransmitPower = NewTXPower;
  97.  
  98.         // Set the new transmit power
  99.         RF69Driver.setTxPower(RFTransmitPower, true);
  100.  
  101.         // Send positive acknowledgement
  102.         CANTS_SendTelecommandAcknowledgement(TelecommandRequest->iChannel, TelecommandRequest->iFromAddress, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  103.        
  104.         break;
  105.  
  106.       case 2: /* NOT IN DB: Set LED color until overwritten */
  107.         // Check the parameter is the correct length
  108.         if (TelecommandRequest->iDataLength != 3)
  109.         {
  110.           Serial.println("Set LED Color TC contains incorrect data length");
  111.           goto LABEL_NEGATIVE_ACKNOWLEDGEMENT;
  112.         }
  113.  
  114.         ColorR = TelecommandRequest->acData[0];
  115.         ColorG = TelecommandRequest->acData[1];
  116.         ColorB = TelecommandRequest->acData[2];
  117.  
  118.         NeoPixelHandle.setPixelColor(0, ColorR, ColorG, ColorB);
  119.         NeoPixelHandle.show();
  120.  
  121.         // Send positive acknowledgement
  122.         CANTS_SendTelecommandAcknowledgement(TelecommandRequest->iChannel, TelecommandRequest->iFromAddress, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  123.        
  124.         break;
  125.  
  126.       case 3: /* NOT IN DB: Perform RP2040 reboot */
  127.         CANTS_SendTelecommandAcknowledgement(TelecommandRequest->iChannel, TelecommandRequest->iFromAddress, CANTS_POSITIVE_ACKNOWLEDGEMENT);
  128.  
  129.         delay(1000);
  130.        
  131.         watchdog_reboot(0, 0, 0);
  132.        
  133.         break;
  134.      
  135.       default:
  136.   LABEL_NEGATIVE_ACKNOWLEDGEMENT:
  137.         // Send negative acknowledgement for unknown telecommand
  138.         CANTS_SendTelecommandAcknowledgement(TelecommandRequest->iChannel, TelecommandRequest->iFromAddress, CANTS_NEGATIVE_ACKNOWLEDGEMENT);
  139.         break;
  140.     }
  141.   }
  142.  
  143.   return BT_SUCCESS;
  144. }
  145.  
  146. void TMTCLoop()
  147. {
  148.   static unsigned long PreviousSendTimeMs = 0;
  149.   unsigned long CurrentTimeMs = millis();
  150.  
  151.   if (CurrentTimeMs >= (PreviousSendTimeMs + CONFIG_COMMS_UTLM_PERIOD_MS))
  152.   {
  153.     CommsStatusBuffer[0] = (uint16_t)((RFFrequency - 800.0) * 100.0); /* Polynomial in the DB is [0, 0.01, 800] */
  154.     CommsStatusBuffer[1] = (uint16_t)RF69Driver.lastRssi();
  155.     CommsStatusBuffer[2] = (uint16_t)RFTransmitPower;
  156.     CommsStatusBuffer[3] = (uint16_t)RF69Driver.rxBad();
  157.    
  158.     PreviousSendTimeMs = CurrentTimeMs;
  159.  
  160.     CANTS_SendUnsolicitedTelemetry(
  161.       1, /* Comms status channel */
  162.       1, /* Send to OBC*/
  163.       (uint8_t*)CommsStatusBuffer, /* Telemetry to send */
  164.       8 /* Data length */
  165.     );
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement