API Reference Hull controller - SergeGit/rc-tank-platform GitHub Wiki

Hull Controller API Reference

1. Overview

The Hull Controller is an Arduino Nano-based subsystem that operates as an I2C slave at address 0x08. It provides low-level control over the RC tank’s hardware components β€” including tracks, turret motors, lights, weapons, and battery monitoring.

Communication with the controller is handled via a command-response protocol over I2C. The Raspberry Pi (Turret Controller) acts as the master.


2. Command Format

Each command consists of two bytes:

Byte Description
1 Command code (defines the operation)
2 Data byte (parameter or value)

Data values are often scaled where 100 = neutral / zero point. For example:

  • 0–99 β†’ Negative range (-100% to 0%)
  • 101–200 β†’ Positive range (0% to +100%)

3. Command List

3.1 Motion Commands

Command Code (Hex) Description Data Range
Tracks Move 0x20 Forward/Backward movement 0–200 β†’ -100% to +100%
Tracks Turn 0x21 Turning control (left/right) 0–200 β†’ -100% to +100%

3.2 Turret Commands

Command Code (Hex) Description Data Range
Turret Rotate 0x10 Rotate turret 0–200 β†’ -100% to +100%
Turret Elevate 0x11 Elevate/depress cannon 0–200 β†’ -100% to +100%

3.3 Weapon Commands

Command Code (Hex) Description Data
Laser Toggle 0x30 Laser sight ON/OFF 0=OFF, 1=ON
IR Toggle 0x31 Infrared emitter ON/OFF 0=OFF, 1=ON
Cannon Fire 0x32 Fire cannon (pulse) Duration (0–255)

3.4 Light Commands

Command Code (Hex) Description Data
Front Lights 0x40 Front light control 0=OFF, 1=ON, 2=BLINK
Rear Lights 0x41 Rear light control 0=OFF, 1=ON, 2=BLINK

3.5 System & Sensor Commands

Command Code (Hex) Description Data
Get Battery 0xE0 Request battery data N/A
Get Position 0xE1 Request position N/A
Get Cannon Status 0xE2 Check if cannon ready N/A
Get GPS 0xE3 Request GPS data N/A
Get All Sensors 0xEF Combined telemetry N/A
Get Status 0xF0 System status flags N/A
Emergency Stop 0xFF Stop/resume all systems 0=STOP, 1=RESUME

4. Response Format

Responses depend on the request command. Data is transmitted back to the I2C master when it issues a read request.

Command Bytes Format
0xE0 Get Battery 5 [Vlow, Vhigh, Type, Relay, Status]
0xE1 Get Position 4 [Xlow, Xhigh, Ylow, Yhigh]
0xE2 Get Cannon Status 1 [0 or 1]
0xE3 Get GPS 8 [Lat_L, Lat_H, Lon_L, Lon_H, Alt_L, Alt_H, Fix, SatCount]
0xEF Get All Sensors 16 Aggregated sensor data
0xF0 Get Status 1 [Status Byte]

5. Status Byte Definition

Each bit in the status byte corresponds to a specific system state flag:

Bit Name Description
0 CANNON_READY Cannon cooldown complete
1 LASER_ON Laser active
2 IR_ON IR emitter active
3 FRONT_LIGHT_ON Front lights ON or BLINK
4 REAR_LIGHT_ON Rear lights ON or BLINK
5 BATTERY_RELAY Battery relay enabled
6 ERROR_STATE Error detected
7 EMERGENCY_STOP E-stop active

6. Data Conversion Examples

6.1 Motion Mapping

Raw  Data  β†’  Mapped Speed (%)
0     β†’  -100
50    β†’  -50
100   β†’   0
150   β†’  +50
200   β†’  +100

6.2 Battery Voltage

Voltage (V) = (raw / 1023.0) * 5.0 * (R1 + R2) / R2
Example: R1=100kΞ©, R2=22kΞ© β†’ Scale β‰ˆ 5.545x

7. Example Code Usage

7.1 Python (Raspberry Pi)

import smbus
bus = smbus.SMBus(1)
ADDR = 0x08

# Move forward 50%
bus.write_byte_data(ADDR, 0x20, 150)

# Turn on front lights
bus.write_byte_data(ADDR, 0x40, 1)

# Fire cannon
bus.write_byte_data(ADDR, 0x32, 128)

# Read battery voltage
bus.write_byte(ADDR, 0xE0)
data = bus.read_i2c_block_data(ADDR, 0, 5)
voltage = (data[0] | (data[1] << 8)) / 100.0
print(f"Battery: {voltage}V")

7.2 Arduino Master Example

#include <Wire.h>
#define HULL_ADDR 0x08

void setup() {
  Wire.begin();
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  sendCommand(0x20, 150); // Move forward
  delay(1000);
  sendCommand(0x21, 100); // Stop turning
}

void sendCommand(byte cmd, byte val) {
  Wire.beginTransmission(HULL_ADDR);
  Wire.write(cmd);
  Wire.write(val);
  byte error = Wire.endTransmission();
  if (error) Serial.printf("I2C error: %d\n", error);
}

8. Timing and Safety

Parameter Value Description
Watchdog Timeout 1s Resets if main loop stalls
Motor Update Interval 20ms PWM refresh rate
Battery Check Interval 2000ms Battery update frequency
Cannon Cooldown 2000ms Prevents double firing
Light Blink Interval 500ms Used in BLINK mode

Safety Notes:

  • Always issue 0xFF,0 (E-stop) before performing maintenance.
  • Ensure logic and motor grounds are common.
  • Avoid sending commands faster than 20ms intervals.

9. Error Handling

9.1 Error Conditions

Error Type Cause System Reaction
Invalid Command Unknown code received Ignored + set error flag
Voltage Fault Battery below threshold Triggers E-stop
Watchdog Reset Loop stall >1s Auto-reset system
I2C Timeout Bus hang detected Controller reset

9.2 Clearing Errors

Errors are cleared automatically when the fault condition is removed. The status byte’s ERROR bit resets when systemError = false.


10. Future Extensions

Feature Description
Checksum Byte Add CRC8 to commands for reliability
Multi-byte Parameters Support 16-bit control inputs
Dynamic Telemetry Configurable sensor response sets
Diagnostic Mode Extended status output for debugging

⚠️ **GitHub.com Fallback** ⚠️