🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Embedded Systems and Microcontrollers for Robots

Robotics & MechatronicsHardware and AutomationđŸŸĸ Free Lesson

Embedded Systems and Microcontrollers for Robots

What You'll Learn

  • 1Convert an ADC code into volts and then into engineering units
  • 2Budget a control loop from sensing through computation to actuation and check its deadline
  • 3Compute PWM period, duty, and timer compare values for a commanded voltage
  • 4Estimate CAN bus frame rate and utilization before it saturates the network

A robot controller fails in ways that datasheets do not warn about: the math is right but the loop took 1.4 ms instead of 1.0 ms, the ADC converted too early after the multiplexer switched, or the bus reached saturation and frames queued behind each other. Embedded control is the discipline of turning equations into scheduled work with bounded time. This lesson works the four arithmetic chains that appear in every review: ADC codes to engineering units, loop timing to deadline margin, duty cycle to timer registers, and messages per second to bus load.

Theory and Foundations

An analog-to-digital converter maps a voltage range onto codes, so one code spans a fixed voltage called the least significant bit. Every engineering unit that follows is a ratio built on that step size.

Sampling must satisfy Nyquist, and the converter itself needs acquisition time after the channel is switched, so a poorly ordered sequence reads the previous channel.

The control loop then has a fixed period, and everything inside it must fit: conversion, control law, and output update.

For independent periodic tasks, the classical fixed-priority bound for tasks is

which equals 0.78 for three tasks, so a sum of worst-case execution over period below that number guarantees every deadline.

Actuation is almost always a pulse-width modulated square wave whose average equals the commanded voltage. The timer peripheral produces it from a clock divider and an auto-reload period.

On the network side, each joint drive publishes state at some rate, and the bus is a shared wire whose capacity is a bit rate. Utilization is simply offered bits against available bits.

with the message rate, the frame length in bits including overhead, and the bus bit rate. Delay matters as much as load: a loop delay subtracts phase at the crossover frequency, degrees, which is why latency budgets appear in control reviews.

Key Formulas

Worked Examples

Peripheral or conceptTypical figureRobot taskFailure to watch for
ADC with sample and hold12 bit, 1 MSa/scurrents, analog sensorsreading during acquisition settling
Timer and PWM20 to 50 kHzmotor driveduty quantization and dead time
DMA transferzero CPU costencoder and ADC batchingpriority inversion under load
CAN bus500 kbit to 1 Mbit/sjoint and IO networksutilization above 70 percent
Interrupt and watchdog1 to 10 ms windowfault reactionmissed deadline or reset loop
Real-time OS tick1 ms typicaltask schedulingjitter from blocking calls

Real-World Examples

  • Field-oriented control - A Cortex-M4 or C2000 part at 120 to 200 MHz runs the Clarke, Park, and PI steps plus three PWM updates inside a 50 microsecond period, keeping CPU load near 60 percent.
  • Safety reaction path - Safe Torque Off is wired through a safety relay so a 10 ms detection and reaction budget is met in hardware, not by software that might hang.
  • micro-ROS agents - An STM32 board running micro-ROS publishes joint states at 100 to 500 Hz over Ethernet while the motor loops stay on a bare-metal core at 20 kHz.
  • Bus segmentation - Arms with 12 or more drives split traffic across two CAN segments or move to EtherCAT, where a distributed clock holds jitter under 1 microsecond.

â„šī¸Latency is a stability term

A 1 ms sensing and computation delay at a 20 Hz crossover costs 360 x 20 x 0.001 = 7.2 degrees of phase margin, so halving the loop delay buys as much stability as retuning gains.

Checking the Deadline Before Deployment

Worst-case execution time measured on a bench only means something once it is compared with the loop period under fixed-priority scheduling theory. Evaluate the utilization in the build rather than in a spreadsheet, print the slack in microseconds, and let continuous integration fail the merge when a feature pushes the sum past the bound. The number that matters is the worst case, because the average hides the interrupt storm that arrives exactly when the network, the encoder, and the converter all fire inside the same millisecond.

#include <stdint.h>
#include <stdio.h>

struct budget { uint32_t sense_us; uint32_t compute_us; uint32_t actuate_us; };

static float utilization(struct budget b, uint32_t period_us) {
    uint32_t worst = b.sense_us + b.compute_us + b.actuate_us;
    return (float)worst / (float)period_us;
}

int main(void) {
    struct budget b = {120, 180, 40};
    float u = utilization(b, 1000);
    printf("u=%.2f slack=%u us schedulable=%s\n", u,
           1000u - (b.sense_us + b.compute_us + b.actuate_us),
           u <= 0.78f ? "yes" : "no");
    return 0;
}

Estimating Bus Load Before Wiring the Network

Network capacity is arithmetic long before it is a cable: multiply the node count by the message rate and the framed length, compare the product with the bit rate, and the answer decides how many drives fit on one segment. Keep the reservation margin explicit, since error frames, remote frames, and diagnostics arrive on top of the planned traffic, and a segment running above about seventy percent starts queueing exactly when a fault makes traffic most urgent. When the budget fails, shorten the frame or raise the bit rate before adding a second wire.

python
def can_budget(nodes, rate_hz, frame_bits, bitrate):
    offered = nodes * rate_hz * frame_bits
    load = offered / bitrate
    frames = offered // frame_bits
    spare = bitrate // frame_bits - frames
    return offered, round(load * 100), spare

print(can_budget(8, 250, 128, 500_000))   # (256000, 51, 1906)
print(can_budget(8, 500, 128, 500_000))   # (512000, 102, -94)

Common Mistakes

Forgetting reference voltage when quoting ADC bits

Fix: Twelve bits means 0.806 mV per count only on a 3.3 V reference; move to 5 V and the step grows to 1.22 mV.

Sizing the average CPU load instead of the worst case

Fix: Average load hides interrupt storms; compare worst-case execution time against the period, then check the scheduling bound.

Ignoring frame overhead on CAN

Fix: A payload of 8 bytes costs about 128 bits once identifiers, CRC, and inter-frame space count, doubling the real load.

Key Takeaways

  • A 12-bit ADC on 3.3 V resolves 0.806 mV per count; code 2048 on a 20 A per 3.3 V scale is 10.0 A.
  • Loop time is sense plus compute plus actuate: 340 microseconds in a 1000 microsecond period leaves utilization at 0.34 against a 0.78 bound.
  • Duty 0.6146 on a 50-tick, 20 kHz timer sets CCR to 31 and outputs 14.88 V from a 24 V bus.
  • Eight drives at 250 Hz with 128-bit frames occupy 51 percent of a 500 kbit/s CAN bus, and tripling the rate saturates it.

Frequently Asked Questions

Is bare metal or an RTOS better for a motor loop?

Keep the current and velocity loops bare metal on a dedicated core or timer interrupt, and move logging, communications, and state machines onto an RTOS. The hard real-time path must never wait on a mutex held by a blocking network call.

Why must the ADC input settle after the multiplexer switches?

The sample-and-hold capacitor has to charge through the source impedance of the newly selected channel. Settling to within half a count takes several time constants, so channel hopping without acquisition time yields cross-talk.

What happens when a deadline is missed?

The control output arrives late, which is equivalent to extra dead time. Phase lag grows and the loop approaches instability, so robust designs monitor execution time and trip a safe state instead of publishing a stale command.

Interview Questions

Q1: How much phase lag does a 1 ms loop delay add at a 20 Hz crossover?

360 x 20 x 0.001 = 7.2 degrees of phase lag, a meaningful fraction of the margin a typical PI loop carries.

Q2: What is the utilization bound for three fixed-priority periodic tasks?

U less than or equal to 3 times (2^(1/3) minus 1) = 0.78; a sum of 0.70 is guaranteed schedulable.

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-16

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Robotics & Mechatronics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement