Advertisement
Guest User

Untitled

a guest
Mar 19th, 2025
67
0
95 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.34 KB | None | 0 0
  1. #include <Nextion.h>
  2. #include <ESP32Servo.h>
  3.  
  4.  
  5. // Define serial communication for Nextion
  6. #define nexSerial Serial2
  7.  
  8. // Create Nextion objects (Page, ID, NAME)
  9. NexButton minusLeftBT = NexButton(4, 5, "MinusLeft");
  10. NexButton plusLeftBT = NexButton(4, 6, "PlusLeft");
  11. NexButton minusRightBT = NexButton(4, 7, "MinusRight");
  12. NexButton plusRightBT = NexButton(4, 8, "PlusRight");
  13. NexButton sAxisBT = NexButton(3, 2, "S_AXIS");
  14. NexButton fAxisBT = NexButton(3, 3, "F_AXIS");
  15. NexButton startSessionBT = NexButton(1, 5, "StartSesion");
  16. NexButton pauseBT = NexButton(7, 4, "PAUSE");
  17. NexButton endBT = NexButton(8, 3, "END");
  18. NexButton resumeBT = NexButton(8, 4, "RESUME");
  19. NexButton minusSpeedBT = NexButton(5, 5, "MinusSpeed");
  20. NexButton plusSpeedBT = NexButton(5, 6, "PlusSpeed");
  21. NexText minAngleTX = NexText(4, 11, "MinAngleTX");
  22. NexText maxAngleTX = NexText(4, 12, "MaxAngleTX");
  23. NexText speedTX = NexText(5, 7, "SpeedTX");
  24. NexButton speedBT = NexButton(0, 6, "SPEED");
  25. NexButton plusRepsBT = NexButton(6, 6, "PlusReps");
  26. NexButton minusRepsBT = NexButton(6, 5, "MinusReps");
  27. NexText repsMinTX = NexText(6, 9, "RepsMinTX");
  28. NexText repsMaxTX = NexText(6, 8, "RepsMaxTX");
  29. NexText repetitionTX = NexText(6, 7, "RepetitionTX");
  30. NexButton repetitionBT = NexButton(0, 7, "REPETITION");
  31. NexText HandTX = NexText(1, 9, "HandTX");
  32. NexText MovementsTX = NexText(1, 10, "MovementsTX");
  33. NexText SpeedTX = NexText(1, 11, "SpeedTX");
  34. NexText RepetitionTX = NexText(1, 12, "RepetitionTX");
  35. NexButton startBT = NexButton(0, 3, "START");
  36. NexButton LeftHandDual = NexButton(2, 5, "LeftHandDual");
  37. NexButton RightHandDual = NexButton(2, 6, "RightHandDual");
  38.  
  39. Servo servoSide;
  40. Servo servoTop;
  41.  
  42.  
  43. int neutralAngleSide = 132;  // Neutral position for servoSide
  44. int neutralAngleTop = 137;   // Neutral position for servoTop
  45.  
  46. // Variables for pause at extremes, repetitions, and speed
  47. int extremePause = 1000;  // Pause duration at extremes (in milliseconds)
  48.  
  49. // Servo specifications
  50. const float maxSpeed = 0.14;                   // Time per 60° at 6V (seconds)
  51. const float maxAnglePerSec = 60.0 / maxSpeed;  // Maximum angle per second at max speed
  52.  
  53. // Homecoming speed
  54. int homeComingSpeed = 30;  // Default homecoming speed set to 30%
  55.  
  56. // Constants for limits of each axis
  57. const int S_AXIS_MAX_LEFT = 0;
  58. const int S_AXIS_MIN_LEFT = -20;
  59. const int S_AXIS_MAX_RIGHT = 40;
  60. const int S_AXIS_MIN_RIGHT = 0;
  61.  
  62. const int F_AXIS_MAX_LEFT = 0;
  63. const int F_AXIS_MIN_LEFT = -5;
  64. const int F_AXIS_MAX_RIGHT = 20;
  65. const int F_AXIS_MIN_RIGHT = 0;
  66.  
  67. const int SPEED_MAX = 100;
  68. const int SPEED_MIN = 0;
  69. const int REPS_MAX = 50;
  70. const int REPS_MIN = 1;
  71.  
  72. // Variables to store the current angles, speed, and repetitions
  73. int s_axis_left = -20;
  74. int s_axis_right = 20;
  75. int f_axis_left = -5;
  76. int f_axis_right = 5;
  77. int currentSpeed = 50;
  78. int currentReps = 10;
  79.  
  80. int remainingReps;
  81.  
  82. // Variables to store the current button states
  83. bool minusLeftBTPressed = false;
  84. bool plusLeftBTPressed = false;
  85. bool minusRightBTPressed = false;
  86. bool plusRightBTPressed = false;
  87. bool minusSpeedBTPressed = false;
  88. bool plusSpeedBTPressed = false;
  89. bool plusRepsBTPressed = false;
  90. bool minusRepsBTPressed = false;
  91. unsigned long minusLeftBTPressTime = 0;
  92. unsigned long plusLeftBTPressTime = 0;
  93. unsigned long minusRightBTPressTime = 0;
  94. unsigned long plusRightBTPressTime = 0;
  95. unsigned long minusSpeedBTPressTime = 0;
  96. unsigned long plusSpeedBTPressTime = 0;
  97. unsigned long plusRepsBTPressTime = 0;
  98. unsigned long minusRepsBTPressTime = 0;
  99.  
  100. const unsigned long SHORT_PRESS_DURATION = 300;
  101. const unsigned long LONG_PRESS_DURATION = 1000;
  102. const unsigned long REPEAT_RATE = 100;
  103. bool isSAxis = true;
  104. bool isRightHand = true;     // Default to right hand
  105. bool sessionPaused = false;  // Tracks whether the session is paused
  106. bool endSession = false;     // Tracks whether the session is canceled
  107.  
  108.  
  109. // Constants for Serial Communication Pins
  110. const int RX_PIN = 16;
  111. const int TX_PIN = 17;
  112.  
  113. NexTouch *nex_listen_list[] = {
  114.   &minusLeftBT,
  115.   &plusLeftBT,
  116.   &minusRightBT,
  117.   &plusRightBT,
  118.   &sAxisBT,
  119.   &fAxisBT,
  120.   &startSessionBT,
  121.   &pauseBT,
  122.   &endBT,
  123.   &resumeBT,
  124.   &minusSpeedBT,
  125.   &plusSpeedBT,
  126.   &speedBT,
  127.   &plusRepsBT,
  128.   &minusRepsBT,
  129.   &repetitionBT,
  130.   &startBT,
  131.   &LeftHandDual,
  132.   &RightHandDual,
  133.   NULL
  134. };
  135.  
  136. // Function declarations
  137. void minusLeftBTPressCallback(void *ptr);
  138. void minusLeftBTReleaseCallback(void *ptr);
  139. void plusLeftBTPressCallback(void *ptr);
  140. void plusLeftBTReleaseCallback(void *ptr);
  141. void minusRightBTPressCallback(void *ptr);
  142. void minusRightBTReleaseCallback(void *ptr);
  143. void plusRightBTPressCallback(void *ptr);
  144. void plusRightBTReleaseCallback(void *ptr);
  145. void minusSpeedBTPressCallback(void *ptr);
  146. void minusSpeedBTReleaseCallback(void *ptr);
  147. void plusSpeedBTPressCallback(void *ptr);
  148. void plusSpeedBTReleaseCallback(void *ptr);
  149. void plusRepsBTPressCallback(void *ptr);
  150. void plusRepsBTReleaseCallback(void *ptr);
  151. void minusRepsBTPressCallback(void *ptr);
  152. void minusRepsBTReleaseCallback(void *ptr);
  153. void sAxisBTCallback(void *ptr);
  154. void fAxisBTCallback(void *ptr);
  155. void startSessionBTCallback(void *ptr);
  156. void pauseBTCallback(void *ptr);
  157. void endBTCallback(void *ptr);
  158. void resumeBTCallback(void *ptr);
  159. void repetitionBTCallback(void *ptr);
  160. void speedBTCallback(void *ptr);  // Added speedBTCallback declaration
  161.  
  162.  
  163.  
  164. void setup() {
  165.   Serial.begin(115200);
  166.   nexSerial.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  167.   nexInit();
  168.  
  169.   // Update the SUMMARY page
  170.   updateSummaryPage();
  171.  
  172.   servoSide.attach(25);
  173.   servoTop.attach(27);
  174.   servoSide.write(neutralAngleSide);
  175.   servoTop.write(neutralAngleTop);
  176.   delay(500);
  177.   Serial.println("Ready to receive commands.");
  178.  
  179.   minusLeftBT.attachPush(minusLeftBTPressCallback, &minusLeftBT);
  180.   minusLeftBT.attachPop(minusLeftBTReleaseCallback, &minusLeftBT);
  181.   plusLeftBT.attachPush(plusLeftBTPressCallback, &plusLeftBT);
  182.   plusLeftBT.attachPop(plusLeftBTReleaseCallback, &plusLeftBT);
  183.   minusRightBT.attachPush(minusRightBTPressCallback, &minusRightBT);
  184.   minusRightBT.attachPop(minusRightBTReleaseCallback, &minusRightBT);
  185.   plusRightBT.attachPush(plusRightBTPressCallback, &plusRightBT);
  186.   plusRightBT.attachPop(plusRightBTReleaseCallback, &plusRightBT);
  187.   sAxisBT.attachPop(sAxisBTCallback, &sAxisBT);
  188.   fAxisBT.attachPop(fAxisBTCallback, &fAxisBT);
  189.   startSessionBT.attachPop(startSessionBTCallback, &startSessionBT);
  190.   pauseBT.attachPush(pauseBTCallback, &pauseBT);
  191.   endBT.attachPop(endBTCallback, &endBT);
  192.   resumeBT.attachPop(resumeBTCallback, &resumeBT);
  193.   minusSpeedBT.attachPush(minusSpeedBTPressCallback, &minusSpeedBT);
  194.   minusSpeedBT.attachPop(minusSpeedBTReleaseCallback, &minusSpeedBT);
  195.   plusSpeedBT.attachPush(plusSpeedBTPressCallback, &plusSpeedBT);
  196.   plusSpeedBT.attachPop(plusSpeedBTReleaseCallback, &plusSpeedBT);
  197.   speedBT.attachPop(speedBTCallback, &speedBT);  // Added speedBTCallback
  198.   plusRepsBT.attachPush(plusRepsBTPressCallback, &plusRepsBT);
  199.   plusRepsBT.attachPop(plusRepsBTReleaseCallback, &plusRepsBT);
  200.   minusRepsBT.attachPush(minusRepsBTPressCallback, &minusRepsBT);
  201.   minusRepsBT.attachPop(minusRepsBTReleaseCallback, &minusRepsBT);
  202.   repetitionBT.attachPop(repetitionBTCallback, &repetitionBT);
  203.   startBT.attachPop(startBTCallback, &startBT);
  204.   LeftHandDual.attachPop(LeftHandDualCallback, &LeftHandDual);
  205.   RightHandDual.attachPop(RightHandDualCallback, &RightHandDual);
  206.  
  207.  
  208.   updateDisplay();
  209.   updateMinMaxDisplay();
  210.   updatespeedTX();
  211. }
  212.  
  213. void loop() {
  214.   nexLoop(nex_listen_list);
  215.   if (sessionPaused) return;
  216.  
  217.   unsigned long currentTime = millis();
  218.  
  219.   if (minusLeftBTPressed && currentTime - minusLeftBTPressTime > LONG_PRESS_DURATION) {
  220.     static unsigned long lastUpdateTime = 0;
  221.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  222.       if (isSAxis) {
  223.         s_axis_left = max(s_axis_left - 1, S_AXIS_MIN_LEFT);
  224.       } else {
  225.         f_axis_left = max(f_axis_left - 1, F_AXIS_MIN_LEFT);
  226.       }
  227.       updateDisplay();
  228.       lastUpdateTime = currentTime;
  229.     }
  230.   }
  231.  
  232.   if (plusLeftBTPressed && currentTime - plusLeftBTPressTime > LONG_PRESS_DURATION) {
  233.     static unsigned long lastUpdateTime = 0;
  234.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  235.       if (isSAxis) {
  236.         s_axis_left = min(s_axis_left + 1, S_AXIS_MAX_LEFT);
  237.       } else {
  238.         f_axis_left = min(f_axis_left + 1, F_AXIS_MAX_LEFT);
  239.       }
  240.       updateDisplay();
  241.       lastUpdateTime = currentTime;
  242.     }
  243.   }
  244.  
  245.   if (minusRightBTPressed && currentTime - minusRightBTPressTime > LONG_PRESS_DURATION) {
  246.     static unsigned long lastUpdateTime = 0;
  247.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  248.       if (isSAxis) {
  249.         s_axis_right = max(s_axis_right - 1, S_AXIS_MIN_RIGHT);
  250.       } else {
  251.         f_axis_right = max(f_axis_right - 1, F_AXIS_MIN_RIGHT);
  252.       }
  253.       updateDisplay();
  254.       lastUpdateTime = currentTime;
  255.     }
  256.   }
  257.  
  258.   if (plusRightBTPressed && currentTime - plusRightBTPressTime > LONG_PRESS_DURATION) {
  259.     static unsigned long lastUpdateTime = 0;
  260.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  261.       if (isSAxis) {
  262.         s_axis_right = min(s_axis_right + 1, S_AXIS_MAX_RIGHT);
  263.       } else {
  264.         f_axis_right = min(f_axis_right + 1, F_AXIS_MAX_RIGHT);
  265.       }
  266.       updateDisplay();
  267.       lastUpdateTime = currentTime;
  268.     }
  269.   }
  270.  
  271.   if (minusSpeedBTPressed && currentTime - minusSpeedBTPressTime > LONG_PRESS_DURATION) {
  272.     static unsigned long lastUpdateTime = 0;
  273.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  274.       currentSpeed = max(currentSpeed - 1, SPEED_MIN);
  275.       updatespeedTX();
  276.       lastUpdateTime = currentTime;
  277.     }
  278.   }
  279.  
  280.   if (plusSpeedBTPressed && currentTime - plusSpeedBTPressTime > LONG_PRESS_DURATION) {
  281.     static unsigned long lastUpdateTime = 0;
  282.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  283.       currentSpeed = min(currentSpeed + 1, SPEED_MAX);
  284.       updatespeedTX();
  285.       lastUpdateTime = currentTime;
  286.     }
  287.   }
  288.  
  289.   // Logic for repetition increment
  290.   if (plusRepsBTPressed && currentTime - plusRepsBTPressTime > LONG_PRESS_DURATION) {
  291.     static unsigned long lastUpdateTime = 0;
  292.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  293.       currentReps = min(currentReps + 1, REPS_MAX);
  294.       updateRepetitionTX();
  295.       lastUpdateTime = currentTime;
  296.     }
  297.   }
  298.  
  299.   // Logic for repetition decrement
  300.   if (minusRepsBTPressed && currentTime - minusRepsBTPressTime > LONG_PRESS_DURATION) {
  301.     static unsigned long lastUpdateTime = 0;
  302.     if (currentTime - lastUpdateTime >= REPEAT_RATE) {
  303.       currentReps = max(currentReps - 1, REPS_MIN);
  304.       updateRepetitionTX();
  305.       lastUpdateTime = currentTime;
  306.     }
  307.   }
  308. }
  309.  
  310. void updateDisplay() {
  311.   nexSerial.print("LeftAngleTX.txt=\"");
  312.   if (isSAxis) {
  313.     nexSerial.print(s_axis_left);
  314.   } else {
  315.     nexSerial.print(f_axis_left);
  316.   }
  317.   nexSerial.write(0xB0);  // Degree symbol
  318.   nexSerial.print("\"");
  319.   nexSerial.write(0xff);
  320.   nexSerial.write(0xff);
  321.   nexSerial.write(0xff);
  322.  
  323.   nexSerial.print("RightAngleTX.txt=\"");
  324.   if (isSAxis) {
  325.     nexSerial.print(s_axis_right);
  326.   } else {
  327.     nexSerial.print(f_axis_right);
  328.   }
  329.   nexSerial.write(0xB0);  // Degree symbol
  330.   nexSerial.print("\"");
  331.   nexSerial.write(0xff);
  332.   nexSerial.write(0xff);
  333.   nexSerial.write(0xff);
  334. }
  335.  
  336. void updatespeedTX() {
  337.   nexSerial.print("SpeedTX.txt=\"");
  338.   nexSerial.print(currentSpeed);
  339.   nexSerial.print("%\"");
  340.   nexSerial.write(0xff);
  341.   nexSerial.write(0xff);
  342.   nexSerial.write(0xff);
  343. }
  344.  
  345. void updateRepetitionTX() {
  346.   nexSerial.print("RepetitionTX.txt=\"");
  347.   nexSerial.print(currentReps);
  348.   nexSerial.print("x\"");
  349.   nexSerial.write(0xff);
  350.   nexSerial.write(0xff);
  351.   nexSerial.write(0xff);
  352. }
  353.  
  354. void updateMinMaxDisplay() {
  355.   char minAngleTXStr[10];
  356.   char maxAngleTXStr[10];
  357.   if (isSAxis) {
  358.     sprintf(minAngleTXStr, "%d", S_AXIS_MIN_LEFT);
  359.     sprintf(maxAngleTXStr, "%d", S_AXIS_MAX_RIGHT);
  360.   } else {
  361.     sprintf(minAngleTXStr, "%d", F_AXIS_MIN_LEFT);
  362.     sprintf(maxAngleTXStr, "%d", F_AXIS_MAX_RIGHT);
  363.   }
  364.   minAngleTX.setText(minAngleTXStr);
  365.   maxAngleTX.setText(maxAngleTXStr);
  366.  
  367.   char minRepsTXStr[10];
  368.   char maxRepsTXStr[10];
  369.   sprintf(minRepsTXStr, "%d", REPS_MIN);
  370.   sprintf(maxRepsTXStr, "%d", REPS_MAX);
  371.   repsMinTX.setText(minRepsTXStr);
  372.   repsMaxTX.setText(maxRepsTXStr);
  373.  
  374.   updateDisplay();
  375.   updatespeedTX();
  376.   updateRepetitionTX();
  377. }
  378.  
  379.  
  380. void updateRepsAfterPress(unsigned long pressTime, int *reps, int minValue, int maxValue, bool increment) {
  381.   unsigned long pressDuration = millis() - pressTime;
  382.   if (pressDuration < LONG_PRESS_DURATION) {
  383.     if (increment) {
  384.       *reps = min(*reps + 1, maxValue);
  385.     } else {
  386.       *reps = max(*reps - 1, minValue);
  387.     }
  388.     updateRepetitionTX();
  389.   }
  390. }
  391.  
  392. void updateNumberAfterPress(unsigned long pressTime, int *number, int minValue, int maxValue, bool increment) {
  393.   unsigned long pressDuration = millis() - pressTime;
  394.   if (pressDuration < LONG_PRESS_DURATION) {
  395.     if (increment) {
  396.       *number = min(*number + 1, maxValue);
  397.     } else {
  398.       *number = max(*number - 1, minValue);
  399.     }
  400.     updateDisplay();
  401.     updatespeedTX();
  402.   }
  403. }
  404.  
  405. void updateSummaryPage() {
  406.   // Update hand information
  407.   nexSerial.print("SUMMARY.HandTX.txt=\"");
  408.   nexSerial.print(isRightHand ? "Right" : "Left");
  409.   nexSerial.print("\"");
  410.   nexSerial.write(0xFF);
  411.   nexSerial.write(0xFF);
  412.   nexSerial.write(0xFF);
  413.  
  414.   // Update MovementsTX
  415.   nexSerial.print("SUMMARY.MovementsTX.txt=\"");
  416.   if (isSAxis) {
  417.     nexSerial.print("S: ");
  418.     nexSerial.print(s_axis_left);
  419.     nexSerial.write(0xB0);  // Degree symbol
  420.     nexSerial.print(" / ");
  421.     nexSerial.print(s_axis_right);
  422.     nexSerial.write(0xB0);  // Degree symbol
  423.   } else {
  424.     nexSerial.print("F: ");
  425.     nexSerial.print(f_axis_left);
  426.     nexSerial.write(0xB0);  // Degree symbol
  427.     nexSerial.print(" / ");
  428.     nexSerial.print(f_axis_right);
  429.     nexSerial.write(0xB0);  // Degree symbol
  430.   }
  431.   nexSerial.print("\"");
  432.   nexSerial.write(0xFF);
  433.   nexSerial.write(0xFF);
  434.   nexSerial.write(0xFF);
  435.  
  436.   // Update SpeedTX
  437.   nexSerial.print("SUMMARY.SpeedTX.txt=\"");
  438.   nexSerial.print(currentSpeed);
  439.   nexSerial.print("%\"");
  440.   nexSerial.write(0xFF);
  441.   nexSerial.write(0xFF);
  442.   nexSerial.write(0xFF);
  443.  
  444.   // Update RepetitionTX
  445.   nexSerial.print("SUMMARY.RepetitionTX.txt=\"");
  446.   nexSerial.print(currentReps);
  447.   nexSerial.print("x\"");
  448.   nexSerial.write(0xFF);
  449.   nexSerial.write(0xFF);
  450.   nexSerial.write(0xFF);
  451. }
  452.  
  453. void moveServo() {
  454.   int minAngle = isSAxis ? s_axis_left : f_axis_left;    // Determine the minimum angle based on the active axis
  455.   int maxAngle = isSAxis ? s_axis_right : f_axis_right;  // Determine the maximum angle based on the active axis
  456.  
  457.   Servo &activeServo = isSAxis ? servoSide : servoTop;  // Select the active servo (servoSide or servoTop)
  458.  
  459.   int lastDisplayedSpeed = -1;  // Track the last displayed speed to avoid redundant updates
  460.   remainingReps = currentReps;  // Initialize the remaining repetitions
  461.  
  462.   for (int i = 1; i <= currentReps; i++) {
  463.     remainingReps = currentReps - i + 1;  // Update remaining reps count
  464.  
  465.     // Check if session is paused or canceled
  466.     if (sessionPaused) {
  467.       homeComing(activeServo, isSAxis);  // Move to neutral position
  468.       while (sessionPaused) {
  469.         // Wait here until resumed (pauseBT toggles sessionPaused to false)
  470.         if (endSession) {
  471.           Serial.println("Session canceled by end button.");
  472.           return;  // Exit the loop entirely
  473.         }
  474.         delay(100);  // Small delay to avoid busy-waiting
  475.       }
  476.     }
  477.     if (endSession) {
  478.       Serial.println("Session canceled by end button.");
  479.       return;  // Exit the loop entirely
  480.     }
  481.  
  482.     // Display remaining repetitions
  483.     char remainingRepsStr[10];
  484.     sprintf(remainingRepsStr, "%dx", remainingReps);
  485.     nexSerial.print("ActiveRepsTX.txt=\"");
  486.     nexSerial.print(remainingRepsStr);
  487.     nexSerial.print("\"");
  488.     nexSerial.write(0xff);
  489.     nexSerial.write(0xff);
  490.     nexSerial.write(0xff);
  491.  
  492.     // Display the current speed
  493.     if (currentSpeed != lastDisplayedSpeed) {
  494.       char activeSpeedStr[10];
  495.       sprintf(activeSpeedStr, "%d%%", currentSpeed);
  496.       nexSerial.print("ActiveSpeedTX.txt=\"");
  497.       nexSerial.print(activeSpeedStr);
  498.       nexSerial.print("\"");
  499.       nexSerial.write(0xff);
  500.       nexSerial.write(0xff);
  501.       nexSerial.write(0xff);
  502.  
  503.       lastDisplayedSpeed = currentSpeed;
  504.     }
  505.  
  506.     // Move to minimum angle
  507.     nexSerial.print("ActiveAngleTX.txt=\"");
  508.     nexSerial.print(minAngle);
  509.     nexSerial.write(0xB0);  // Degree symbol
  510.     nexSerial.print("\"");
  511.     nexSerial.write(0xff);
  512.     nexSerial.write(0xff);
  513.     nexSerial.write(0xff);
  514.  
  515.     moveToAngle(activeServo, minAngle, isSAxis);  // Move servo
  516.     delay(extremePause);                          // Pause at extreme
  517.  
  518.     // Move to maximum angle
  519.     nexSerial.print("ActiveAngleTX.txt=\"");
  520.     nexSerial.print(maxAngle);
  521.     nexSerial.write(0xB0);  // Degree symbol
  522.     nexSerial.print("\"");
  523.     nexSerial.write(0xff);
  524.     nexSerial.write(0xff);
  525.     nexSerial.write(0xff);
  526.  
  527.     moveToAngle(activeServo, maxAngle, isSAxis);  // Move servo
  528.     delay(extremePause);                          // Pause at extreme
  529.   }
  530.  
  531.   // Move servo to neutral position after finishing
  532.   homeComing(activeServo, isSAxis);
  533.  
  534.   // Navigate to page 0
  535.   nexSerial.print("page 0");
  536.   nexSerial.write(0xff);
  537.   nexSerial.write(0xff);
  538.   nexSerial.write(0xff);
  539.  
  540.   Serial.println("Servo movement complete. Navigated to page 0.");
  541. }
  542.  
  543. void moveToAngle(Servo &servo, int targetUserAngle, bool isSideServo) {
  544.   int targetAngle = convertToServoAngle(targetUserAngle, isSideServo);
  545.   int currentAngle = servo.read();
  546.   Serial.print("Moving servo to: ");
  547.   Serial.println(targetAngle);
  548.  
  549.   int step = (currentAngle < targetAngle) ? 1 : -1;
  550.   while (currentAngle != targetAngle) {
  551.     currentAngle += step;
  552.     servo.write(currentAngle);
  553.  
  554.     // Speed-adjusted delay
  555.     float speedFactor = currentSpeed / 100.0;    // Convert percentage to a factor (0.0 to 1.0)
  556.     int dynamicDelay = (int)(10 / speedFactor);  // Scale the delay inversely with speed
  557.     delay(dynamicDelay);
  558.   }
  559.   servo.write(targetAngle);
  560. }
  561.  
  562.  
  563.  
  564.  
  565. void homeComing(Servo &servo, bool isSideServo) {
  566.   int neutralAngle = isSideServo ? neutralAngleSide : neutralAngleTop;  // Determine neutral angle
  567.   int currentAngle = servo.read();                                      // Get the current servo angle
  568.   Serial.println("Returning servo to neutral position...");
  569.  
  570.   // Determine the direction of movement
  571.   int step = (currentAngle < neutralAngle) ? 1 : -1;
  572.  
  573.   // Adjust speed based on homeComingSpeed (scale delay inversely)
  574.   float speedFactor = homeComingSpeed / 100.0;  // Convert speed percentage to a factor (0.0 to 1.0)
  575.   int dynamicDelay = (int)(10 / speedFactor);   // Scale delay inversely with speed factor
  576.  
  577.   // Gradually move servo to neutral position
  578.   while (currentAngle != neutralAngle) {
  579.     currentAngle += step;
  580.     servo.write(currentAngle);
  581.     delay(dynamicDelay);  // Apply speed-controlled delay
  582.   }
  583.  
  584.   // Ensure servo is precisely at the neutral position
  585.   servo.write(neutralAngle);
  586.   Serial.println("Servo returned to neutral position.");
  587. }
  588.  
  589.  
  590.  
  591. float calculateSpeed() {
  592.   return maxAnglePerSec * (currentSpeed / 100.0);
  593. }
  594.  
  595. int convertToServoAngle(int userAngle, bool isSideServo) {
  596.   if (isSideServo) {
  597.     return neutralAngleSide + userAngle;
  598.   } else {
  599.     return neutralAngleTop + userAngle;
  600.   }
  601. }
  602.  
  603.  
  604. // Button press and release callbacks
  605. void minusLeftBTPressCallback(void *ptr) {
  606.   minusLeftBTPressed = true;
  607.   minusLeftBTPressTime = millis();
  608. }
  609.  
  610. void minusLeftBTReleaseCallback(void *ptr) {
  611.   minusLeftBTPressed = false;
  612.   if (isSAxis) {
  613.     updateNumberAfterPress(minusLeftBTPressTime, &s_axis_left, S_AXIS_MIN_LEFT, S_AXIS_MAX_LEFT, false);
  614.   } else {
  615.     updateNumberAfterPress(minusLeftBTPressTime, &f_axis_left, F_AXIS_MIN_LEFT, F_AXIS_MAX_LEFT, false);
  616.   }
  617.   updateSummaryPage();
  618. }
  619.  
  620. void plusLeftBTPressCallback(void *ptr) {
  621.   plusLeftBTPressed = true;
  622.   plusLeftBTPressTime = millis();
  623. }
  624.  
  625. void plusLeftBTReleaseCallback(void *ptr) {
  626.   plusLeftBTPressed = false;
  627.   if (isSAxis) {
  628.     updateNumberAfterPress(plusLeftBTPressTime, &s_axis_left, S_AXIS_MIN_LEFT, S_AXIS_MAX_LEFT, true);
  629.   } else {
  630.     updateNumberAfterPress(plusLeftBTPressTime, &f_axis_left, F_AXIS_MIN_LEFT, F_AXIS_MAX_LEFT, true);
  631.   }
  632.   updateSummaryPage();
  633. }
  634.  
  635. void minusRightBTPressCallback(void *ptr) {
  636.   minusRightBTPressed = true;
  637.   minusRightBTPressTime = millis();
  638. }
  639.  
  640. void minusRightBTReleaseCallback(void *ptr) {
  641.   minusRightBTPressed = false;
  642.   if (isSAxis) {
  643.     updateNumberAfterPress(minusRightBTPressTime, &s_axis_right, S_AXIS_MIN_RIGHT, S_AXIS_MAX_RIGHT, false);
  644.   } else {
  645.     updateNumberAfterPress(minusRightBTPressTime, &f_axis_right, F_AXIS_MIN_RIGHT, F_AXIS_MAX_RIGHT, false);
  646.   }
  647.   updateSummaryPage();
  648. }
  649.  
  650. void plusRightBTPressCallback(void *ptr) {
  651.   plusRightBTPressed = true;
  652.   plusRightBTPressTime = millis();
  653. }
  654.  
  655. void plusRightBTReleaseCallback(void *ptr) {
  656.   plusRightBTPressed = false;
  657.   if (isSAxis) {
  658.     updateNumberAfterPress(plusRightBTPressTime, &s_axis_right, S_AXIS_MIN_RIGHT, S_AXIS_MAX_RIGHT, true);
  659.   } else {
  660.     updateNumberAfterPress(plusRightBTPressTime, &f_axis_right, F_AXIS_MIN_RIGHT, F_AXIS_MAX_RIGHT, true);
  661.   }
  662.   updateSummaryPage();
  663. }
  664.  
  665. void minusSpeedBTPressCallback(void *ptr) {
  666.   minusSpeedBTPressed = true;
  667.   minusSpeedBTPressTime = millis();
  668. }
  669.  
  670. void minusSpeedBTReleaseCallback(void *ptr) {
  671.   minusSpeedBTPressed = false;
  672.   updateNumberAfterPress(minusSpeedBTPressTime, &currentSpeed, SPEED_MIN, SPEED_MAX, false);
  673.   updateSummaryPage();  // Update SUMMARY page
  674. }
  675.  
  676. void plusSpeedBTReleaseCallback(void *ptr) {
  677.   plusSpeedBTPressed = false;
  678.   updateNumberAfterPress(plusSpeedBTPressTime, &currentSpeed, SPEED_MIN, SPEED_MAX, true);
  679.   updateSummaryPage();  // Update SUMMARY page
  680. }
  681.  
  682.  
  683. void plusSpeedBTPressCallback(void *ptr) {
  684.   plusSpeedBTPressed = true;
  685.   plusSpeedBTPressTime = millis();
  686. }
  687.  
  688. void plusRepsBTPressCallback(void *ptr) {
  689.   plusRepsBTPressed = true;
  690.   plusRepsBTPressTime = millis();
  691. }
  692.  
  693. void plusRepsBTReleaseCallback(void *ptr) {
  694.   plusRepsBTPressed = false;
  695.   updateRepsAfterPress(plusRepsBTPressTime, &currentReps, REPS_MIN, REPS_MAX, true);
  696.   updateSummaryPage();  // Update SUMMARY page
  697. }
  698.  
  699. void minusRepsBTReleaseCallback(void *ptr) {
  700.   minusRepsBTPressed = false;
  701.   updateRepsAfterPress(minusRepsBTPressTime, &currentReps, REPS_MIN, REPS_MAX, false);
  702.   updateSummaryPage();  // Update SUMMARY page
  703. }
  704.  
  705.  
  706. void minusRepsBTPressCallback(void *ptr) {
  707.   minusRepsBTPressed = true;
  708.   minusRepsBTPressTime = millis();
  709. }
  710.  
  711. void sAxisBTCallback(void *ptr) {
  712.   isSAxis = true;
  713.   updateMinMaxDisplay();
  714.   updateSummaryPage();  // Update SUMMARY page
  715. }
  716.  
  717. void fAxisBTCallback(void *ptr) {
  718.   isSAxis = false;
  719.   updateMinMaxDisplay();
  720.   updateSummaryPage();  // Update SUMMARY page
  721. }
  722.  
  723. void startSessionBTCallback(void *ptr) {
  724.   Serial.println("Starting servo movement...");
  725.   moveServo();
  726. }
  727.  
  728. void pauseBTCallback(void *ptr) {
  729.   sessionPaused = !sessionPaused;  // Toggle pause state
  730.   if (sessionPaused) {
  731.     Serial.println("Session paused.");
  732.     homeComing(isSAxis ? servoSide : servoTop, isSAxis);  // Move to neutral
  733.   } else {
  734.     Serial.println("Session resumed.");
  735.   }
  736. }
  737.  
  738. void endBTCallback(void *ptr) {
  739.   endSession = true;  // Set the session cancel flag
  740.   Serial.println("Session canceled. Moving servos to neutral...");
  741.  
  742.   // Move servos to neutral position
  743.   if (isSAxis) {
  744.     homeComing(servoSide, true);  // Neutral for s-axis servo
  745.   } else {
  746.     homeComing(servoTop, false);  // Neutral for f-axis servo
  747.   }
  748.  
  749.   Serial.println("Servos moved to neutral position. Session ended.");
  750. }
  751.  
  752.  
  753.  
  754. void resumeBTCallback(void *ptr) {
  755.   sessionPaused = false;
  756.   Serial.println("Session resumed.");
  757. }
  758.  
  759. void speedBTCallback(void *ptr) {
  760.   updatespeedTX();
  761. }
  762.  
  763. void repetitionBTCallback(void *ptr) {
  764.   updateMinMaxDisplay();
  765. }
  766.  
  767. void startBTCallback(void *ptr) {
  768.   Serial.println("START button pressed.F");
  769. }
  770.  
  771. void LeftHandDualCallback(void *ptr) {
  772.   isRightHand = false;  // Set to left hand
  773.   Serial.println("Left hand selected.");
  774.  
  775.   // Update Nextion display states
  776.   nexSerial.print("HAND.LeftHandDual.val=1");  // Turn LeftHandDual ON
  777.   nexSerial.write(0xFF);
  778.   nexSerial.write(0xFF);
  779.   nexSerial.write(0xFF);
  780.  
  781.   nexSerial.print("HAND.RightHandDual.val=0");  // Turn RightHandDual OFF
  782.   nexSerial.write(0xFF);
  783.   nexSerial.write(0xFF);
  784.   nexSerial.write(0xFF);
  785.  
  786.   // Update HandDual button value
  787.   nexSerial.print("HOME.HandDual.val=1");  // Left hand (value 1)
  788.   nexSerial.write(0xFF);
  789.   nexSerial.write(0xFF);
  790.   nexSerial.write(0xFF);
  791.  
  792.   updateSummaryPage();  // Reflect changes on SUMMARY page
  793. }
  794.  
  795. void RightHandDualCallback(void *ptr) {
  796.   isRightHand = true;  // Set to right hand
  797.   Serial.println("Right hand selected.");
  798.  
  799.   // Update Nextion display states
  800.   nexSerial.print("HAND.RightHandDual.val=1");  // Turn RightHandDual ON
  801.   nexSerial.write(0xFF);
  802.   nexSerial.write(0xFF);
  803.   nexSerial.write(0xFF);
  804.  
  805.   nexSerial.print("HAND.LeftHandDual.val=0");  // Turn LeftHandDual OFF
  806.   nexSerial.write(0xFF);
  807.   nexSerial.write(0xFF);
  808.   nexSerial.write(0xFF);
  809.  
  810.   // Update HandDual button value
  811.   nexSerial.print("HOME.HandDual.val=0");  // Right hand (value 0)
  812.   nexSerial.write(0xFF);
  813.   nexSerial.write(0xFF);
  814.   nexSerial.write(0xFF);
  815.  
  816.   updateSummaryPage();  // Reflect changes on SUMMARY page
  817. }
  818.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement