Power
This step takes the robot off the USB cable. The 2S LiPo goes through the power switch to a 5 V buck that feeds the LaunchPad, and a divider lets ADC1 read the battery voltage. The firmware reads it every 10 ms from the tick, filters it, and won't start a run once the battery is flat. A LiPo pulled below 3.3 V per cell is damaged, and a sagging battery leaves the motors less headroom to catch a fall.
Hardware: the carrier board (perfboard with female headers for the LaunchPad), a 2S LiPo with a JST-PH lead, the slide switch, the 5 V buck, a Schottky diode (1N5817 or SS14), 10 kΩ and 3.3 kΩ resistors, a 100 nF capacitor. See Parts.
Wire it
| From | To |
|---|---|
| Battery + | Switch, middle pin |
Switch, outer pin (switched battery, VBAT) | Buck VIN, and the 10 kΩ resistor. In Step 4 the motor driver's VM joins here |
Buck VOUT | Diode anode. Cathode to the LaunchPad's +5V pin (J3 pin 21) |
| 10 kΩ / 3.3 kΩ junction | PD3 (J3 pin 26), and the 100 nF to ground |
Battery −, buck GND, 3.3 kΩ, 100 nF | LaunchPad GND (J3 pin 22) |
Check before you plug in the LaunchPad
A reversed JST lead or a buck set to the wrong voltage destroys the board. With the LaunchPad out of its headers, switch on and measure: +5V pin about 4.7 V (5 V less the diode), PD3 about a quarter of the battery voltage (2.08 V at 8.4 V). Then switch off and plug the board in.
What you add
| File | What it does |
|---|---|
battery.c | ADC1 sequencer 3 on AIN4 (PD3), 64× hardware averaging. The tick reads one result and starts the next every 10 ms, low-pass filters it, and latches the battery dead after 1 s below the cutoff |
battery.h | Divider scale, the warning (7.0 V) and cutoff (6.6 V) thresholds, and the state: usb, ok, LOW, DEAD |
tick.c | Changed: calls battery_tick() |
main.c | Changed: prints the battery at boot and in Test mode, and refuses every mode but Test on a dead battery |
clock.c, ui.c and uart.c are unchanged from Step 2.
// The robot's main program. Each step fills in a mode; Test prints the tick's health and the battery.
#include <stdio.h>
#include "TM4C123.h"
#include "battery.h"
#include "clock.h"
#include "tick.h"
#include "uart.h"
#include "ui.h"
enum { MODE_BALANCE, MODE_DRIVE, MODE_CALIBRATE, MODE_TEST, N_MODES };
static const uint32_t mode_colour[N_MODES] = { LED_GREEN, LED_BLUE, LED_YELLOW, LED_MAGENTA };
static const char *const mode_name[N_MODES] = { "balance", "drive", "calibrate", "test" };
static const char *const batt_name[] = { "usb", "ok", "LOW", "DEAD" };
static void print_battery(void)
{
uint32_t mv = battery_mv();
printf("bat %lu.%02lu V %s", mv / 1000, mv % 1000 / 10, batt_name[battery_state()]);
}
// Once a second until SW2: uptime, battery, and how much of each 1 ms tick the handler uses
static void test_status(void)
{
uint32_t next = millis();
while (!button_pressed(BTN_SW2))
{
if ((int32_t)(millis() - next) < 0) continue;
next += 1000;
uint32_t max = tick_cycles_max();
printf("t=%lu s ", millis() / 1000);
print_battery();
printf(" tick: max %lu cyc (%lu.%02lu%% of 1 ms)\n",
max, max * 100 / 80000, max * 10000 / 80000 % 100);
}
}
// A dead battery: three seconds of red, then back to the menu
static void refuse(void)
{
printf("refused: ");
print_battery();
printf(", charge it\n");
for (int i = 0; i < 6; i++)
{
led_set(i % 2 ? LED_OFF : LED_RED);
delay_ms(500);
}
}
static void run(uint32_t mode)
{
// Test still runs on a dead battery, so you can watch the voltage
if (mode != MODE_TEST && battery_state() == BATT_DEAD)
{
refuse();
return;
}
printf("mode: %s\n", mode_name[mode]);
if (battery_state() == BATT_LOW) printf("battery low: charge it after this run\n");
if (mode == MODE_TEST)
test_status();
else
while (!button_pressed(BTN_SW2)) {} // Not written yet
printf("menu\n");
}
int main(void)
{
clock_init();
uart_init();
ui_init();
battery_init(); // Before the tick, which reads it
tick_init();
printf("\nbalance-bot: %lu MHz, built " __DATE__ " " __TIME__ "\n", (unsigned long)(CPU_HZ / 1000000));
print_battery();
printf("\n");
uint32_t mode = MODE_BALANCE;
while (1)
{
mode = ui_menu(mode_colour, N_MODES, mode);
run(mode);
}
}// The battery voltage, on PD3 (AIN4) through a 10 k / 3.3 k divider. It's read by ADC1, which
// leaves ADC0 free for later. The tick starts one conversion every 10 ms.
#include "TM4C123.h"
#include "battery.h"
#define PERIOD_MS 10U // One reading every 10 ms
#define DEAD_MS 1000U // Below the cutoff this long: dead. Motor current dips are shorter.
#define PD3 (1U << 3)
#define ADC_SS3 (1U << 3)
#define SSCTL_END0 (1U << 1) // Sequence ends after step 0
#define SSCTL_IE0 (1U << 2) // Step 0 sets RIS when done
#define SAC_64X 6U // Hardware averages 64 conversions into one result
static volatile uint32_t mv_x16; // Filtered voltage × 16
static volatile uint32_t dead; // Latched until the battery is unplugged
static uint32_t count, below_ms;
static uint32_t raw_to_mv(uint32_t raw) { return raw * BATT_FULL_SCALE_MV / 4096; }
void battery_init(void)
{
SYSCTL->RCGCGPIO |= (1U << 3); // Port D
SYSCTL->RCGCADC |= (1U << 1); // ADC1
while (!(SYSCTL->PRGPIO & (1U << 3))) {}
while (!(SYSCTL->PRADC & (1U << 1))) {}
GPIOD->DIR &= ~PD3;
GPIOD->DEN &= ~PD3;
GPIOD->AFSEL |= PD3;
GPIOD->AMSEL |= PD3; // PD3 is AIN4
ADC1->ACTSS &= ~ADC_SS3; // Sequencer 3 off while it's set up
ADC1->EMUX &= ~(0xFU << 12); // Started by software (PSSI)
ADC1->SSMUX3 = 4; // AIN4
ADC1->SSCTL3 = SSCTL_IE0 | SSCTL_END0;
ADC1->SAC = SAC_64X;
ADC1->ACTSS |= ADC_SS3;
// One reading now, so the filter starts at the battery's voltage instead of 0
ADC1->PSSI = ADC_SS3;
while (!(ADC1->RIS & ADC_SS3)) {}
mv_x16 = raw_to_mv(ADC1->SSFIFO3 & 0xFFF) * 16;
ADC1->ISC = ADC_SS3;
ADC1->PSSI = ADC_SS3; // The tick reads this one
}
void battery_tick(void)
{
if (++count < PERIOD_MS) return;
count = 0;
if (!(ADC1->RIS & ADC_SS3)) return; // 64 conversions take 64 µs, so this is always done
uint32_t mv = raw_to_mv(ADC1->SSFIFO3 & 0xFFF);
ADC1->ISC = ADC_SS3;
ADC1->PSSI = ADC_SS3; // Start the next; it's read 10 ms from now
mv_x16 = mv_x16 - mv_x16 / 16 + mv; // Low-pass: 16 readings, a 160 ms time constant
uint32_t avg = mv_x16 / 16;
if (avg < BATT_USB_MV)
{
dead = 0; // Battery unplugged or switched off: start over
below_ms = 0;
}
else if (avg < BATT_CUTOFF_MV)
{
below_ms += PERIOD_MS;
if (below_ms >= DEAD_MS) dead = 1;
}
else
below_ms = 0;
}
uint32_t battery_mv(void) { return mv_x16 / 16; }
enum batt_state battery_state(void)
{
uint32_t mv = battery_mv();
if (dead) return BATT_DEAD;
if (mv < BATT_USB_MV) return BATT_NONE;
if (mv < BATT_WARN_MV) return BATT_LOW;
return BATT_OK;
}#pragma once
#include <stdint.h>
// The divider: 10 k from the battery, 3.3 k to ground. A full-scale reading (3.3 V at PD3)
// is 3.3 V × 13.3 / 3.3 = 13.3 V at the battery. Calibrate it against a multimeter.
#define BATT_FULL_SCALE_MV 13300U
#define BATT_WARN_MV 7000U // 3.5 V per cell: finish the run, then charge
#define BATT_CUTOFF_MV 6600U // 3.3 V per cell: stop, or the cells are damaged
#define BATT_USB_MV 1000U // Below this there is no battery: running from USB
enum batt_state { BATT_NONE, BATT_OK, BATT_LOW, BATT_DEAD };
void battery_init(void);
void battery_tick(void); // Called every 1 ms from the tick
uint32_t battery_mv(void); // Filtered battery voltage, in mV
enum batt_state battery_state(void);// The 1 kHz system tick. Everything periodic runs from SysTick_Handler:
// buttons and the battery now, then the IMU, encoders and balance loop in later steps.
#include "TM4C123.h"
#include "battery.h"
#include "clock.h"
#include "cycles.h"
#include "tick.h"
#include "ui.h"
#define STCTRL_ENABLE (1U << 0)
#define STCTRL_INTEN (1U << 1)
#define STCTRL_CLKSRC (1U << 2)
static volatile uint32_t ms;
static volatile uint32_t cost_last, cost_max;
void tick_init(void)
{
cycles_init();
SysTick->CTRL = 0;
SysTick->LOAD = CPU_HZ / TICK_HZ - 1;
SysTick->VAL = 0;
SysTick->CTRL = STCTRL_CLKSRC | STCTRL_INTEN | STCTRL_ENABLE;
}
void SysTick_Handler(void)
{
uint32_t start = cycles();
ms++;
ui_tick();
battery_tick();
uint32_t cost = cycles() - start;
cost_last = cost;
if (cost > cost_max) cost_max = cost;
}
uint32_t millis(void) { return ms; }
void delay_ms(uint32_t n)
{
uint32_t start = ms;
while (ms - start < n) {}
}
uint32_t tick_cycles_last(void) { return cost_last; }
uint32_t tick_cycles_max(void)
{
uint32_t m = cost_max;
cost_max = 0;
return m;
}Notes
- ADC1, not ADC0.
SACaverages every conversion on its ADC, so the battery's 64× averaging would slow down anything else that shared it. ADC1 keeps ADC0 free for later. - The divider: 8.4 V × 3.3 / 13.3 = 2.08 V, well inside the ADC's 3.3 V. Its 2.5 kΩ source impedance is too high for the ADC's sample capacitor on its own; the 100 nF holds the voltage steady while it samples. The divider sits after the switch, so its 0.6 mA doesn't drain a switched-off battery.
- Calibration: resistor tolerance puts the reading a few percent off. Set
BATT_FULL_SCALE_MVto 13300 × (multimeter reading) / (printed reading). - The 1 s window: stalled motors pull the battery down for a moment. The filter (160 ms) and the 1 s wait keep that from tripping the cutoff; a flat battery stays low.
- On USB alone the divider reads 0 V, so the state is
usband nothing is refused. Switching the battery off (below 1 V) also clears a latchedDEAD, so a fresh battery works without a reset. - The cutoff only refuses to run. The LaunchPad and the buck still drain the battery while the switch is on, so switch off after every run.
Test
- Check the wiring as in the danger box above, then plug the LaunchPad in.
- No USB cable, switch on: the LED menu comes up. The robot runs from its battery.
- Connect USB and the terminal (as in Step 2), with the switch still on, and press RESET. The boot line is followed by
bat 7.xx V ok. Measure the battery with the multimeter and calibrateBATT_FULL_SCALE_MVuntil the two agree within about 50 mV. - Start Test. Each line now shows the battery. Switch the battery off: within a second the line says
usb. - Cutoff check, without draining a battery: set
BATT_CUTOFF_MVto 9000 and flash. With the battery on, Test showsDEADafter a second. Leave Test and start Balance: the terminal printsrefused: …and the LED flashes red for 3 s. Switch the battery off and on: back took. Put 6600 back. - Compare the tick's max cost with your Step 2 baseline. The battery adds a short read every 10th tick.