diff --git a/wifi-test/wifi-with-ui.ino b/wifi-test/wifi-with-ui.ino index 2c880ab84163745daa45e1e647b4c85906ccba04..8c1e2b4465bb8db431833b92dfa2aea68cf600b3 100644 --- a/wifi-test/wifi-with-ui.ino +++ b/wifi-test/wifi-with-ui.ino @@ -35,7 +35,7 @@ int pinkyTarget = 90; // Movement parameters const int STEP_DELAY = 5; // Delay between steps (ms) -const int STEP_SIZE = 15; // Size of each step in degrees +const int STEP_SIZE = 15; // Size of each step in degrees void setup() { Serial.begin(9600); @@ -72,6 +72,49 @@ void setup() { server.begin(); } +// Move all servos together for fist motion +void moveAllServosSmooth(int targetPos) { + int maxSteps = 0; + + // Calculate the maximum number of steps needed + maxSteps = max(maxSteps, abs(indexTarget - indexPos)); + maxSteps = max(maxSteps, abs(middleTarget - middlePos)); + maxSteps = max(maxSteps, abs(ringTarget - ringPos)); + maxSteps = max(maxSteps, abs(pinkyTarget - pinkyPos)); + + if (maxSteps > 0) { + // Calculate step size for each finger + float indexStep = (float)(indexTarget - indexPos) / maxSteps; + float middleStep = (float)(middleTarget - middlePos) / maxSteps; + float ringStep = (float)(ringTarget - ringPos) / maxSteps; + float pinkyStep = (float)(pinkyTarget - pinkyPos) / maxSteps; + + // Move all fingers together + for (int i = 1; i <= maxSteps; i++) { + // Calculate next position for each finger + int newIndexPos = indexPos + round(indexStep * i); + int newMiddlePos = middlePos + round(middleStep * i); + int newRingPos = ringPos + round(ringStep * i); + int newPinkyPos = pinkyPos + round(pinkyStep * i); + + // Move all servos + index_f.write(newIndexPos); + middle.write(newMiddlePos); + ring.write(newRingPos); + pinky.write(newPinkyPos); + + delay(STEP_DELAY); + } + + // Update the current positions + indexPos = indexTarget; + middlePos = middleTarget; + ringPos = ringTarget; + pinkyPos = pinkyTarget; + } +} + +// Individual servo movement for single finger control void moveServoSmooth(Servo &servo, int ¤tPos, int targetPos) { if (currentPos < targetPos) { for (int pos = currentPos; pos <= targetPos; pos += STEP_SIZE) { @@ -89,11 +132,29 @@ void moveServoSmooth(Servo &servo, int ¤tPos, int targetPos) { } void loop() { - // Handle smooth servo movements - if (indexPos != indexTarget) moveServoSmooth(index_f, indexPos, indexTarget); - if (middlePos != middleTarget) moveServoSmooth(middle, middlePos, middleTarget); - if (ringPos != ringTarget) moveServoSmooth(ring, ringPos, ringTarget); - if (pinkyPos != pinkyTarget) moveServoSmooth(pinky, pinkyPos, pinkyTarget); + // Check if any finger needs to move to its target position + bool needToMove = (indexPos != indexTarget) || + (middlePos != middleTarget) || + (ringPos != ringTarget) || + (pinkyPos != pinkyTarget); + + // If all fingers have the same target (fist motion), move them simultaneously + bool sameFistMotion = (indexTarget == middleTarget && + middleTarget == ringTarget && + ringTarget == pinkyTarget); + + if (needToMove) { + if (sameFistMotion) { + // Move all fingers together + moveAllServosSmooth(indexTarget); + } else { + // Move individual fingers + if (indexPos != indexTarget) moveServoSmooth(index_f, indexPos, indexTarget); + if (middlePos != middleTarget) moveServoSmooth(middle, middlePos, middleTarget); + if (ringPos != ringTarget) moveServoSmooth(ring, ringPos, ringTarget); + if (pinkyPos != pinkyTarget) moveServoSmooth(pinky, pinkyPos, pinkyTarget); + } + } WiFiClient client = server.available(); @@ -127,20 +188,31 @@ void loop() { client.println(".button:hover { background: #45a049; }"); client.println(".preset { background: #2196F3; }"); client.println(".reset { background: #f44336; }"); - client.println(".wave { background: #ff9800; }"); + client.println(".fist { background: #9C27B0; }"); client.println("</style>"); client.println("</head>"); client.println("<body>"); client.println("<h1>Robotic Hand Control Panel</h1>"); + // Fist motion controls (added at top for prominence) + client.println("<div class='control-group'>"); + client.println("<h2>Fist Motion Controls</h2>"); + client.println("<button class='button fist' onclick='allFingers(0)'>Close Fist</button>"); + client.println("<button class='button fist' onclick='allFingers(180)'>Open Hand</button>"); + client.println("<button class='button fist' onclick='fistGrasp()'>Grasp Motion</button>"); + client.println("</div>"); + // Individual finger controls String fingers[] = {"Index", "Middle", "Ring", "Pinky"}; int positions[] = {indexPos, middlePos, ringPos, pinkyPos}; String endpoints[] = {"index", "middle", "ring", "pinky"}; + client.println("<div class='control-group'>"); + client.println("<h2>Individual Finger Controls</h2>"); + for(int i = 0; i < 4; i++) { - client.println("<div class='control-group'>"); + client.println("<div style='margin-bottom:10px;'>"); client.println("<h3>" + fingers[i] + " Finger</h3>"); client.println("<input type='range' class='slider' min='0' max='180' value='" + String(positions[i]) + "' "); client.println("oninput='updateFinger(\"" + endpoints[i] + "\", this.value)'>"); @@ -150,19 +222,12 @@ void loop() { client.println("<button class='button' onclick='updateFinger(\"" + endpoints[i] + "\", 180)'>Open</button>"); client.println("</div>"); } - - // Preset controls - client.println("<div class='control-group'>"); - client.println("<h3>Preset Controls</h3>"); - client.println("<button class='button preset' onclick='allFingers(0)'>Close All</button>"); - client.println("<button class='button preset' onclick='allFingers(180)'>Open All</button>"); - client.println("<button class='button reset' onclick='resetPositions()'>Reset to 90°</button>"); client.println("</div>"); - // Wave motion controls + // Additional preset controls client.println("<div class='control-group'>"); - client.println("<h3>Wave Motion</h3>"); - client.println("<button class='button wave' onclick='startWave()'>Start Wave</button>"); + client.println("<h3>Other Controls</h3>"); + client.println("<button class='button reset' onclick='resetPositions()'>Reset to 90°</button>"); client.println("</div>"); // JavaScript @@ -172,23 +237,20 @@ void loop() { client.println("}"); client.println("function allFingers(angle) {"); - client.println(" ['index', 'middle', 'ring', 'pinky'].forEach(finger => {"); - client.println(" updateFinger(finger, angle);"); - client.println(" });"); + client.println(" fetch('/fist/' + angle);"); client.println("}"); client.println("function resetPositions() {"); client.println(" allFingers(90);"); client.println("}"); - client.println("function startWave() {"); - client.println(" let fingers = ['index', 'middle', 'ring', 'pinky'];"); - client.println(" let delay = 0;"); - client.println(" fingers.forEach(finger => {"); - client.println(" setTimeout(() => updateFinger(finger, 180), delay);"); - client.println(" setTimeout(() => updateFinger(finger, 0), delay + 1000);"); - client.println(" delay += 500;"); - client.println(" });"); + client.println("function fistGrasp() {"); + client.println(" // Open hand first"); + client.println(" allFingers(180);"); + client.println(" // Close after a delay"); + client.println(" setTimeout(() => allFingers(0), 1500);"); + client.println(" // Open again after another delay"); + client.println(" setTimeout(() => allFingers(180), 3000);"); client.println("}"); client.println("</script>"); @@ -223,7 +285,16 @@ void processRequest(String request) { Serial.println("Command: " + command); // Parse command and set target positions - if (command.startsWith("/index/")) { + if (command.startsWith("/fist/")) { + // Set all fingers to same position + int angle = constrain(command.substring(6).toInt(), 0, 180); + indexTarget = angle; + middleTarget = angle; + ringTarget = angle; + pinkyTarget = angle; + Serial.println("Moving all fingers to " + String(angle)); + } + else if (command.startsWith("/index/")) { indexTarget = constrain(command.substring(7).toInt(), 0, 180); Serial.println("Moving index to " + String(indexTarget)); }