/*
* Vantage Dry Run — 6-DOF Arm · Phase 5 Electrical PoC (Wokwi simulation)
* ----------------------------------------------------------------------
* Build: ESP32 (Wi-Fi MCU) -> 7 hobby servos (direct GPIO PWM)
* Mirrors: the 7 actuated joints of 6_dof_arm.urdf
* J1 base yaw · J2 shoulder · J3 elbow · J4 forearm roll
* J5 wrist pitch · J6 tool roll · J7 stylus pitch
*
* This is the firmware side of the same "one pipeline, five triggers" idea:
* the web app computes joint angles (IK / autonomous PIN), sends them over
* Wi-Fi, and this MCU turns them into servo PWM. Here we expose an HTTP API
* (/joint, /pin) AND run an autonomous self-test + PIN-entry demo on boot so
* the arm visibly moves in the simulator.
*
* NOTE on power: in Wokwi, servo V+ is tied to the ESP32 VIN (5V) for
* simplicity. On real hardware you MUST power servos from a separate 6V / 5A+
* supply (UBEC) with a COMMON GROUND — never from the ESP32's 3V3/5V pin.
*/
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
// ----- Joint / servo map (matches the URDF joint order) -----------------
const int NUM_JOINTS = 7;
const int SERVO_PINS[NUM_JOINTS] = { 13, 14, 27, 26, 25, 33, 32 };
const char* JOINT_NAMES[NUM_JOINTS] = {
"J1 base yaw", "J2 shoulder", "J3 elbow", "J4 forearm roll",
"J5 wrist pitch", "J6 tool roll", "J7 stylus pitch"
};
Servo joints[NUM_JOINTS];
// ----- Poses (degrees, 0-180, 90 = centre) ------------------------------
// In the real app these come from the IK solver; here they are illustrative
// presets, one per key of the 6-key panel (key.config.json).
const int HOME_POSE[NUM_JOINTS] = { 90, 90, 90, 90, 90, 90, 90 };
const int KEY_POSE[6][NUM_JOINTS] = {
{ 70, 75, 115, 90, 80, 90, 95 }, // key 1
{ 80, 72, 118, 90, 78, 90, 95 }, // key 2
{ 90, 70, 120, 90, 76, 90, 95 }, // key 3
{ 100, 75, 115, 90, 80, 90, 95 }, // key 4
{ 110, 72, 118, 90, 78, 90, 95 }, // key 5
{ 120, 70, 120, 90, 76, 90, 95 }, // key 6
};
WebServer server(80);
// ----- Motion helpers ---------------------------------------------------
void applyPose(const int pose[NUM_JOINTS]) {
for (int i = 0; i < NUM_JOINTS; i++) joints[i].write(pose[i]);
}
// Approach -> descend (touch) -> retract at one key. Approach axis is -z, so
// the "press" dips the shoulder + stylus, then lifts back off.
void pressKey(int key /* 1..6 */) {
if (key < 1 || key > 6) { Serial.printf(" (skip invalid key %d)\n", key); return; }
const int* p = KEY_POSE[key - 1];
Serial.printf(" -> approach KEY %d\n", key);
applyPose(p);
delay(600);
joints[1].write(p[1] + 12); // shoulder dip (downward touch)
joints[6].write(p[6] + 15); // stylus press
delay(400);
joints[1].write(p[1]); // retract
joints[6].write(p[6]);
delay(300);
Serial.printf(" -> KEY %d pressed\n", key);
}
void enterPin(const String& pin) {
Serial.printf("Entering PIN \"%s\"\n", pin.c_str());
for (size_t i = 0; i < pin.length(); i++) pressKey(pin[i] - '0');
applyPose(HOME_POSE);
Serial.println("PIN entry complete; returned HOME.");
}
// ----- HTTP handlers (the "remote over Wi-Fi" interface) ----------------
void handleRoot() {
server.send(200, "text/plain",
"6-DOF arm controller online.\n"
"GET /joint?n=1..7&angle=0..180\n"
"GET /pin?code=314625\n");
}
void handleJoint() {
int n = server.arg("n").toInt();
int a = server.arg("angle").toInt();
if (n >= 1 && n <= NUM_JOINTS) {
a = constrain(a, 0, 180);
joints[n - 1].write(a);
Serial.printf("HTTP: %s -> %d deg\n", JOINT_NAMES[n - 1], a);
server.send(200, "text/plain", "ok");
} else {
server.send(400, "text/plain", "bad joint (use n=1..7)");
}
}
void handlePin() {
String code = server.arg("code");
enterPin(code);
server.send(200, "text/plain", "pin entered: " + code);
}
void setup() {
Serial.begin(115200);
delay(200);
// ESP32Servo needs its LEDC timers allocated before attaching servos.
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
for (int i = 0; i < NUM_JOINTS; i++) {
joints[i].setPeriodHertz(50); // standard 50 Hz servo frame
joints[i].attach(SERVO_PINS[i], 500, 2400); // 0.5-2.4 ms pulse range
}
applyPose(HOME_POSE);
Serial.println("Servos attached and homed.");
// ----- Wi-Fi link -----------------------------------------------------
Serial.print("Connecting to Wi-Fi");
WiFi.begin("Wokwi-GUEST", "", 6); // Wokwi virtual AP
while (WiFi.status() != WL_CONNECTED) { delay(300); Serial.print("."); }
Serial.println();
Serial.print("Wi-Fi connected. IP: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/joint", handleJoint);
server.on("/pin", handlePin);
server.begin();
Serial.println("HTTP server started on port 80.");
// ----- Autonomous demo so the arm moves in the simulator --------------
Serial.println("\n--- Startup self-test: sweeping all 7 joints ---");
for (int i = 0; i < NUM_JOINTS; i++) {
Serial.printf("Testing %s (GPIO%d)\n", JOINT_NAMES[i], SERVO_PINS[i]);
joints[i].write(60); delay(250);
joints[i].write(120); delay(250);
joints[i].write(90); delay(250);
}
Serial.println("\n--- Demo: autonomous PIN entry 3-1-4-6-2-5 ---");
enterPin("314625");
Serial.println("\nReady. Send HTTP commands, e.g. /pin?code=531642");
}
void loop() {
server.handleClient();
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4