Implementation Hull Controller - SergeGit/rc-tank-platform GitHub Wiki
/src
├── main.cpp
├── config.h
├── main.h
├── bms.cpp / bms.h
├── motor_control.cpp / motor_control.h
├── tracks.cpp / tracks.h
├── turret.cpp / turret.h
├── weapons.cpp / weapons.h
├── lights.cpp / lights.h
Each module is responsible for setup, update, and specific command handling.
main.h includes:
#include <Arduino.h>
#include <Wire.h>
#include "config.h"
#include "bms.h"
#include "motor_control.h"
#include "tracks.h"
#include "turret.h"
#include "weapons.h"
#include "lights.h"Define system states, command codes, and shared global variables.
In config.h:
- Include all pin mappings (A0–A7, D2–D13)
- Define
#define I2C_ADDRESS 0x08 - Add motor thresholds, ramp parameters, and timing constants.
- Follow wiring table in documentation.
- Verify all grounds are connected.
- Separate logic and motor power.
- Ensure 4.7kΩ pull-ups on SDA/SCL lines.
void setup() {
Wire.begin(I2C_ADDRESS);
setupBatteryManagement();
setupMotorControl();
setupTracks();
setupTurret();
setupWeapons();
setupLights();
setupWatchdogTimer();
}void loop() {
updateBatteryManagement();
updateMotors();
updateWeapons();
updateLights();
checkSystemState();
wdt_reset();
}void receiveEvent(int howMany) {
if (howMany >= 2) {
byte command = Wire.read();
byte data = Wire.read();
processCommand(command, data);
}
}void requestEvent() {
Wire.write(responseBuffer, responseLength);
}Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);switch (cmd) {
case 0x20: handleTracksMove(data); break;
case 0x21: handleTracksTurn(data); break;
case 0x10: handleTurretRotate(data); break;
case 0x11: handleTurretElevate(data); break;
case 0x30: handleLaserToggle(data); break;
case 0x31: handleIrToggle(data); break;
case 0x32: handleCannonFire(data); break;
case 0x40: handleFrontLights(data); break;
case 0x41: handleRearLights(data); break;
case 0xE0: prepareBatteryResponse(); break;
case 0xF0: prepareStatusResponse(); break;
case 0xFF: if (data==0) emergencyStop(); else resumeFromEmergencyStop(); break;
default: setErrorState(); break;
}void emergencyStop() {
stopAllMotors();
disableWeapons();
setSystemState(STATE_EMERGENCY_STOP);
}
void resumeFromEmergencyStop() {
setSystemState(STATE_NORMAL);
}- Read voltage from analog input.
- Convert via divider formula.
- Update every 2 seconds.
- Provide
updateBatteryResponseData()for I2C requests.
- Implement registration and control of L293D and Cytron motors.
- Include ramping, thresholds, and voltage compensation.
- Periodically call
updateMotors().
- Control movement via differential drive logic.
- Support both command mode and direct arcade drive.
- Use L293D channels for rotation/elevation.
- Map command byte to signed speed.
- Stop automatically on error or E-stop.
- Fire cannon with timed pulse and cooldown.
- Laser and IR toggles via digital pins.
- Implement
getIsCannonReady()for safety.
- Manage ON/OFF/BLINK states.
-
updateLights()handles blink timing.
void prepareStatusResponse() {
responseBuffer[0] = buildStatusByte();
responseLength = 1;
}byte buildStatusByte() {
byte status = 0;
if (getIsCannonReady()) status |= (1 << 0);
if (getIsLaserOn()) status |= (1 << 1);
if (getIsIrOn()) status |= (1 << 2);
if (getFrontLightState()) status |= (1 << 3);
if (getRearLightState()) status |= (1 << 4);
if (isRelayEnabled) status |= (1 << 5);
if (systemError) status |= (1 << 6);
if (systemState == STATE_EMERGENCY_STOP) status |= (1 << 7);
return status;
}- Disconnect motors.
- Verify I2C device at
0x08(i2cdetect -y 1). - Confirm battery reading response.
- Lights → Weapons → Tracks → Turret.
- Validate PWM outputs with oscilloscope or LED indicator.
Use Python script from documentation to issue commands and verify replies.
- Issue
Emergency Stopcommand and confirm full system halt. - Disconnect I2C → observe watchdog recovery.
| Enhancement | Description |
|---|---|
| Diagnostics output | Serial print subsystem states for debugging |
| Battery type detection | Adjust cutoff voltage automatically |
| Error logging | Store last 10 errors in EEPROM |
| I2C checksum | Add data integrity validation |
| Startup self-test | Spin-test motors, flash LEDs, validate sensors |