ENES100 OTV
An autonomous vehicle for ENES100's water-sampling mission. An overhead camera tells it where it is in the arena. At the pool it lowers an arm, measures the water's depth, checks it for pollution and pumps a sample. Then it follows the wall past the obstacles and drives under the limbo to the goal.
I'm the only electrical engineer on the team. I wire the robot and write most of its code. My teammates each write a subsystem as its own Arduino sketch, and I merge those into one codebase and fix whatever breaks. I test everything in ENES100's browser simulator first. A single build flag switches the same code between the simulator and the real robot.
- 40/40
- randomized simulator runs passed
- 1,620
- lines of firmware
- 7
- bench tests, one per subsystem
The OTV in 3D
From the team's Onshape CAD. Arduino, breadboard and wiring added from photos.
- 01 / 06
The robot
A plywood deck on two drive wheels and a caster, 21.7 cm wide, 24.1 cm long with the arm down and 12.8 cm tall. It has to stay under 3 kg and fit under the limbo.
- 02 / 06
Drive
Two JGY370 worm-gear motors, 6 V and 30 RPM, each turning an 8 cm wheel directly, with a caster at the back. Running the sides at different speeds steers it and lets it turn in place.
- 03 / 06
Brains
An Arduino UNO R4 WiFi. The overhead vision system tracks the ArUco marker on top and sends position and heading over Wi-Fi. It has to stop within 150 mm of the pool.
- 04 / 06
Arm
A printed arm between two SG90 servos. One motion swings it down over the pool, so the sensors at the tip face the water and the intake tube dips in.
- 05 / 06
Depth
The HC-SR04 at the tip times a ping off the water to measure how deep it is. The reading has to be right within 4 mm.
- 06 / 06
Sample
A peristaltic pump, switched by a MOSFET on D10, pulls at least 20 mL up the tube. The sample has to stay on board until the end of the run.
Select a part to see what it does, how it's wired and the code that runs it.
The arena's overhead camera reads an ArUco marker on top of the robot and sends its position and heading over Wi-Fi. Every navigation decision uses that position. Watches for obstacles ahead so the robot can steer around them. Each reading is the median of several pings, so a single bad echo won't stop the robot. Points down at the water from a known height. The depth is that height minus the distance to the surface. Reads the water's color through filtered channels to check for pollution. It uses six pins, more than any other part. The same C++ runs on the real robot and in ENES100's browser simulator, chosen by a build flag. GitHub Actions compiles both versions on every push. Two DRV8871 H-bridges drive the motors. PWM is capped at 153 of 255 because the motors are rated for 6 V and the pack is 9.6 V. Two SG90 servos move together one degree at a time, which keeps their current spikes from browning out the 5 V rail they share. A MOSFET on D10 turns the pump on and off to pull the water sample. A 9.6 V NiMH pack powers the motor drivers directly and feeds the Arduino's VIN pin, where its onboard regulator brings it down to 5 V.// Turn in place to an absolute heading. PWM is proportional to the
// heading error, clamped to MIN_TURN_PWM..MAX_PWM.
// Positive error = counter-clockwise = left wheel back, right forward.
bool turnTo(float heading) {
unsigned long start = millis();
while (true) {
if (!waitForVision(VISION_LOST_MS)) {
stopDrive();
logMsg("turnTo: vision lost");
return false;
}
if (millis() - start >= MOVE_TIMEOUT_MS) {
stopDrive();
logMsg("turnTo: timeout");
return false;
}
float err = wrapAngle(heading - Enes100.getTheta());
if (fabs(err) <= TURN_TOL_RAD) {
stopDrive();
return true;
}
int pwm = constrain((int)(TURN_GAIN * fabs(err)), MIN_TURN_PWM, MAX_PWM);
if (err > 0) setDrive(-pwm, pwm);
else setDrive(pwm, -pwm);
delay(LOOP_MS);
}
}
// Front HC-SR04: ping at most every US_PING_GAP_MS (old echoes), and
// only report an obstacle after FRONT_HITS_NEEDED close pings in a row.
// No echo (-1) = nothing in range = clear.
if (millis() - gFrontPingMs < US_PING_GAP_MS) return false;
gFrontPingMs = millis();
float mm = pingMM(PIN_FRONT_US_TRIG, PIN_FRONT_US_ECHO);
if (mm >= 0 && mm < FRONT_OBSTACLE_MM) gFrontHits++;
else gFrontHits = 0;
if (gFrontHits < FRONT_HITS_NEEDED) return false;
gFrontHits = 0; // consumed: the next stop needs fresh close pings
return true;float pingMM(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH, US_TIMEOUT_US);
if (duration == 0) return -1;
return US_MM_PER_US * duration / 2.0;
}
float readDistanceMM() {
float readings[US_SAMPLES];
int valid = 0;
for (int i = 0; i < US_SAMPLES; i++) {
float mm = pingMM(PIN_US_TRIG, PIN_US_ECHO);
if (mm >= 0) readings[valid++] = mm;
delay(US_PING_GAP_MS);
}
if (valid == 0) return -1;
return medianOf(readings, valid);
}// Median of the first n values (sorts v in place). Ignores outliers that
// would drag an average off.
static float medianOf(float *v, int n) {
for (int i = 1; i < n; i++) {
float key = v[i];
int j = i - 1;
while (j >= 0 && v[j] > key) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = key;
}
if (n % 2 == 1) return v[n / 2];
return (v[n / 2 - 1] + v[n / 2]) / 2.0;
}
static int readColorPW(int s2, int s3) {
digitalWrite(PIN_COLOR_S2, s2);
digitalWrite(PIN_COLOR_S3, s3);
float pws[COLOR_SAMPLES];
int valid = 0;
for (int i = 0; i < COLOR_SAMPLES; i++) {
unsigned long pw = pulseIn(PIN_COLOR_OUT, LOW, COLOR_TIMEOUT_US);
if (pw > 0) pws[valid++] = pw;
}
delay(COLOR_SETTLE_MS);
if (valid == 0) return 0;
return (int)medianOf(pws, valid);
}// Adiel's rule: polluted if ANY channel's pulse width is below the
// threshold (a strong non-black color). Threshold is uncalibrated.
// 0 = no reading on that channel, so it never counts as a color.
static bool pwSeesColor(int pw) { return pw > 0 && pw < COLOR_POLLUTED_THRESH; }
// Drive layer: the ONLY code that touches motor pins or Tank.
// Navigation calls setDrive()/stopDrive() and nothing else, so this
// file is the single switch point between simulator and robot.
// PWM is -255..255 per side (+ = forward), clamped to +/-MAX_PWM.#if SIMvoid setDrive(int left, int right) {
Tank.setLeftMotorPWM(constrain(left, -MAX_PWM, MAX_PWM));
Tank.setRightMotorPWM(constrain(right, -MAX_PWM, MAX_PWM));
}#elsevoid setDrive(int left, int right) {
writeMotor(PIN_LEFT_IN1, PIN_LEFT_IN2, constrain(left, -MAX_PWM, MAX_PWM), LEFT_INVERT);
writeMotor(PIN_RIGHT_IN1, PIN_RIGHT_IN2, constrain(right, -MAX_PWM, MAX_PWM), RIGHT_INVERT);
}
// ---------------- Robot: one DRV8871 per motor ----------------
// Forward = PWM on IN1, 0 on IN2. Reverse = the opposite.
// Stop = both 0 (coast).
//
// analogWrite is used on BOTH pins in every state, including writing 0.
// On the UNO R4, mixing digitalWrite and analogWrite on the same pin can
// leave it stuck in PWM mode, so these pins never see digitalWrite.
static void writeMotor(int in1, int in2, int pwm, bool invert) {
if (invert) pwm = -pwm;
if (pwm > 0) {
analogWrite(in1, pwm);
analogWrite(in2, 0);
} else if (pwm < 0) {
analogWrite(in1, 0);
analogWrite(in2, -pwm);
} else {
analogWrite(in1, 0);
analogWrite(in2, 0);
}
}
static void writeArm(int angle) {
armServo1.write(angle);
armServo2.write(SERVO2_MIRRORED ? 180 - angle : angle);
}// Sweep 1 degree at a time to limit current spikes on the shared 5 V rail.
static void moveArmTo(int target) {
while (armAngle != target) {
armAngle += (target > armAngle) ? 1 : -1;
writeArm(armAngle);
delay(SERVO_STEP_MS);
}
}
// ---- Pump (from Walter's Arduino_Pump.ino) ----
// Fixes: D10 MOSFET gate (D6 is the right motor); no fake trigger on D2
// (D2 is the front ultrasonic TRIG). PUMP_ML_PER_S is still unmeasured.
void pumpRunMs(unsigned long ms) {
digitalWrite(PIN_PUMP_GATE, HIGH);
delay(ms);
digitalWrite(PIN_PUMP_GATE, LOW);
}void pumpDispenseML(float ml) {
pumpRunMs((unsigned long)(ml / PUMP_ML_PER_S * 1000.0));
}
// ---- Drive (DO NOT RAISE MAX_PWM without asking Micah) ----
// 6 V motors on a 9.6 V pack: cap at 60% (0.60 * 255 = 153).
const int MAX_PWM = 153;
The route my code drives in ENES100's simulator, drawn to scale on the 4 × 2 m arena. The robot starts in a random square and samples the pool in the other one. It then drives up to the top wall, follows it through the 25 cm gap past both rows of obstacles, and cuts down to the limbo lane to reach the goal. I checked 40 randomized runs against the robot's footprint. It never went more than 1 mm past a wall, and it cleared the obstacles by 1.6 to 2 cm.