Skip to content

Add unified ESP32 BLE sketch combining closed-loop steering actuator control and ESC throttle output#1

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/combine-ble-motor-esc-control
Open

Add unified ESP32 BLE sketch combining closed-loop steering actuator control and ESC throttle output#1
Copilot wants to merge 3 commits into
mainfrom
copilot/combine-ble-motor-esc-control

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 17, 2026

This PR adds a single ESP32 Arduino sketch that merges BLE-driven steering actuator control (with potentiometer feedback) and brushless truck ESC control via servo-style PWM. It preserves the existing BLE command protocol/UUIDs while adding ESC arming and fail-safe throttle behavior.

  • New unified sketch

    • Added BLEActuatorEscUnified/BLEActuatorEscUnified.ino.
    • Combines:
      • BLE server + command handling from BLEMotorCombined
      • Closed-loop steering state machine (LEFT/RIGHT/FWD/BACK/STOP) with target-based actuator control
      • ESC output using ESP32Servo + writeMicroseconds()
  • BLE protocol compatibility

    • Kept:
      • SERVICE_UUID = 12345678-1234-1234-1234-123456789012
      • CHARACTERISTIC_UUID = 87654321-4321-4321-4321-210987654321
    • Preserved ACK behavior: notify 0x80 | cmd.
  • ESC integration + safety

    • Added ESC signal output on GPIO14 (non-conflicting with GPIO35/25/26/27 used by steering path).
    • Uses typical configurable pulse bounds (1000–2000us) instead of legacy 2800us max.
    • Boot behavior: arm ESC by holding minimum throttle for a fixed delay.
    • Disconnect behavior: fail-safe to steering stop + ESC minimum throttle.
    • Command-driven throttle model:
      • FWD: ramp to capped forward throttle and hold
      • BACK / STOP: ramp down to minimum throttle
  • ESP32 PWM handling

    • Steering H-bridge enable switched to ESP32 LEDC (ledcAttach/ledcWrite) for explicit ESP32-compatible PWM control.
  • Documentation

    • Updated README.md with a brief section describing the new unified sketch and default ESC pin.
// ACK remains protocol-compatible
uint8_t ack = 0x80 | (uint8_t)cmd;
pCharacteristic->setValue(&ack, 1);
pCharacteristic->notify();

// ESC safety defaults
const int ESC_MIN_US = 1000;
const int ESC_MAX_US = 2000;
const int ESC_THROTTLE_CAP_US = 1500;
Original prompt

Create a single unified Arduino sketch for ESP32 that combines:

  1. BLE command receive logic and steering linear actuator closed-loop control (pot feedback + H-bridge PWM) currently in BLEMotorCombined/BLEMotorCombined.ino on main.
  2. ESC control for a brushless motor using the Servo library / servo-style PWM waveform output (arming + throttle control) as in trucks_motor_control.cpp on branch feature/trucks-motor-control.

Repository: uscmakers/SkateMo
Base branch: main

Constraints / requirements:

  • Target board is ESP32 (Arduino core).
  • Must keep BLE server with the same SERVICE_UUID and CHARACTERISTIC_UUID as BLEMotorCombined.ino.
  • Must preserve existing steering state machine behavior:
    • LEFT -> steer left to LEFT_TARGET
    • RIGHT -> steer right to RIGHT_TARGET
    • FWD -> straight (MID_TARGET) (but now also used for throttle unless changed)
    • BACK -> stop steering (and/or throttle handling as designed)
    • STOP -> stop steering and stop throttle
  • Add ESC control using ESP32-compatible Servo library (ESP32Servo). Use writeMicroseconds.
  • Implement safe behavior:
    • On BLE disconnect: stop actuator motor AND set ESC signal to minimum throttle.
    • On boot: arm the ESC by sending minimum throttle for a defined delay.
  • Choose a safe default ESC signal pin that does not conflict with GPIO35, 25, 26, 27 used for actuator in BLEMotorCombined. Prefer a common PWM-capable ESP32 pin (e.g., GPIO14 or GPIO13). Document it.
  • Use reasonable and typical ESC pulse range defaults (1000–2000 us). If the old code used 2800 us, do not blindly keep it; instead set defaults to 1000–2000 with a configurable cap and explain in comments.
  • Provide a simple throttle model driven by BLE commands:
    • FWD: ramp up to a capped throttle (e.g., THROTTLE_US = 1400–1600) and hold
    • BACK or STOP: ramp down to MIN_US
    • Optionally support incremental throttle if repeated commands are received.
  • Ensure compilation for ESP32 and avoid analogWrite if necessary; prefer LEDC PWM for H-bridge enable pin if needed. If keeping analogWrite, ensure it is supported in the ESP32 Arduino core; otherwise implement LEDC setup.

Deliverables:

  • Add a new sketch folder in the repo root (Arduino style) named BLEActuatorEscUnified with file BLEActuatorEscUnified.ino containing the combined code.
  • Update README.md briefly to mention the new unified sketch and what it does.

Acceptance criteria:

  • The new sketch compiles for ESP32.
  • BLE connects, receives commands, sends ACK (0x80 | cmd).
  • Steering actuator closed-loop control still works.
  • ESC is armed on startup and throttle responds safely to BLE commands.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: In this current repo and the current branch. Is there a unfied code the combines the linear actuator control with the code that writes waveforms (using the servo library) to control an ESC (electronic speed controller) to power a brushless motor?

Assistant: I can’t verify that from here yet, because I don’t currently have access to uscmakers/SkateMo via the code-search tools (“repo inaccessible or not found”).

If you do have access and can share either:

  1. a GitHub URL to the repo (or confirm it’s public), or
  2. the specific branch name + a few relevant file paths (e.g., Arduino .ino files), or
  3. paste the directory listing / the key sketches,

then I can answer precisely whether there’s a single unified sketch/module that combines:

  • linear actuator control (often H-bridge / relays / limit switches / feedback pot, etc.), and
  • ESC control via Servo library (typically Servo.h, attach(), writeMicroseconds(), arming sequence).

What I’ll check once I can access the code:

  • Any file importing Servo.h and also containing actuator-related pins/logic in the same file.
  • Any shared “main” sketch that includes both subsystems via headers/modules.
  • Separate sketches (e.g., actuator_control.ino and esc_test.ino) that are not actually unified.

Reply with the repo link (or make it public temporarily), and tell me the default branch (e.g., main), and I’ll locate the exact file(s) and point you to them.

User: @uscmakers/SkateMo

Assistant: No — in the uscmakers/SkateMo repo on the main branch (at least in the code currently present in the directories we inspected), there isn’t a single unified sketch that combines linear actuator control and ESC control via Servo-library waveform output.

What is present are separate sketches for different subsystems:

  • Linear actuator / steering-style closed-loop control (pot feedback + H-bridge PWM):
    BLEMotorCombined/BLEMotorCombined.ino
    This reads a potentiometer (analogRead) and drives a motor through an H-bridge using digitalWrite + analogWrite based on BLE commands. It does not include Servo/ESC waveform generation.

  • Linear actuator basic serial “up/down” control:
    `MakersActuator/MakersA...

This pull request was created from Copilot chat.

Copilot AI and others added 2 commits April 17, 2026 22:28
Copilot AI changed the title [WIP] Combine BLE command logic and ESC control for ESP32 Add unified ESP32 BLE sketch combining closed-loop steering actuator control and ESC throttle output Apr 17, 2026
Copilot AI requested a review from ycjohnp April 17, 2026 22:32
@ycjohnp ycjohnp marked this pull request as ready for review April 17, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants