Skip to main content
emobility.academy
B.Tech / BEVehicle dynamics & safetyIntermediate About 5 weeksKit + add-ons

Road Incline Detection and Hill-Hold Assist Logic

Build a hill-hold assist that measures road incline with the MPU6050 and briefly holds the vehicle on a slope by commanding motor torque when the brake is released. The hold torque scales with slope, times out safely and hands over to the throttle smoothly.

Uses: I2C · PWM

The problem

On a slope, an electric scooter rolls back in the moment between releasing the brake and opening the throttle. Hill-hold logic detects the incline and applies just enough torque to stop rollback, which needs a reliable slope estimate and careful handover.

Objectives

  • Estimate pitch angle and grade from the accelerometer with filtering
  • Detect standstill and brake release with emulated speed and brake inputs
  • Command hold torque proportional to grade with a time limit
  • Hand over smoothly from hold torque to throttle demand

What you need

From the EV Embedded Development Kit

  • STM32F103C8T6 microcontroller unit (Arm Cortex-M3)
  • 3-axis accelerometer and gyroscope (MPU6050, I2C)
  • Motor driver for PWM motor-control experiments
  • Analog potentiometer input
  • 4×3 matrix keypad
  • 16×2 LCD
  • RGB LEDs and a buzzer

Software

  • STM32CubeIDE with HAL I2C, TIM, ADC and GPIO
  • Embedded C
  • Python with pandas and matplotlib

Extra parts

  • A small brushed DC motor within the motor driver's rating — The kit has a motor driver but no motor; it shows the hold torque command.

Key concepts

Grade
Grade in per cent is 100 times the tangent of the pitch angle; a 10% slope is about 5.7 degrees.
Hold torque
The force needed to stop rollback is mass times g times the sine of the slope angle, converted to motor torque through the wheel radius and gearing.
Static pitch from the accelerometer
At standstill the accelerometer measures only gravity, so pitch from atan2 is accurate; while accelerating it is corrupted, so pitch is only latched at standstill.
Emulated speed
There is no wheel-speed sensor on the kit, so vehicle speed comes from a simple model driven by throttle and slope.

Step-by-step guide

  1. 1

    Requirements

    Write the rules: hill hold activates only at standstill on grades above 3%, when the brake, a held keypad key, is released. The hold lasts at most 3 s, then fades out over 1 s with a warning beep, and any throttle demand larger than the hold torque takes over immediately. Choose example vehicle parameters, namely mass with rider, wheel radius and gear ratio, for the vehicle model. Draw the state diagram with Drive, Braked, Hold and Fade.

    Checkpoint: Written rules and a state diagram (Drive, Braked, Hold, Fade).

  2. 2

    Board setup

    Use the DIP switches to enable the MPU6050, motor driver, potentiometer, keypad, LCD and RGB LEDs. In CubeMX configure the 72 MHz clock, I2C at 400 kHz for the MPU6050, a 20 kHz PWM channel and direction pins for the motor driver, ADC on the pot, keypad GPIOs and a 100 Hz control loop timer. Check that WHO_AM_I reads 0x68 and that a fixed duty drives the motor before writing any logic.

    Checkpoint: MPU6050 WHO_AM_I reads 0x68 and PWM output is present.

  3. 3

    Accelerometer and calibration

    Set the accelerometer to ±2 g for the best resolution, since only gravity matters at standstill, and enable the digital low-pass filter. Calibrate the offsets on a surface you have checked is level with a spirit level. Mount the board so its x-axis points along the vehicle's forward direction, and mark the orientation on the board so tests are repeatable. Check the pitch reads zero within half a degree after calibration.

    Checkpoint: Level reads 0 degrees pitch within half a degree.

  4. 4

    Pitch and grade

    Compute pitch from the accelerometer with atan2 and smooth it with a low-pass filter. Latch the pitch only when the modelled speed is zero and the gyro rate is small, because acceleration corrupts the reading while moving. Convert to grade as 100 times the tangent of pitch and show it on the LCD. Test with blocks of known height under one end of a board of known length and compare with the calculated grade.

    Checkpoint: Raising one end of the board on a block of known height gives the expected grade within 1%.

  5. 5

    Vehicle model

    Integrate speed from motor force minus the slope force, which is mass times g times the sine of the slope, minus rolling resistance. Allow speed to go negative so the model shows rollback when the brake is off and torque is too low, and stop the model when the brake is held. Run it at 100 Hz in fixed point. Check that releasing the brake on a tilted board without hill hold shows rollback in the model.

    Checkpoint: With no hold logic, releasing the brake on a tilted board shows rollback in the model.

  6. 6

    Hold logic

    When the brake is released at standstill on a grade above the threshold, set the motor duty to the hold torque computed from the slope force, the wheel radius and the gear ratio, scaled to duty for your motor. Start a 3 s timer; when it expires, fade the torque to zero over 1 s while beeping, so the rider knows to brake or accelerate. Re-applying the brake cancels the hold.

    Checkpoint: With hold active, modelled rollback is near zero until the fade begins.

  7. 7

    Throttle handover

    Compute the rider's throttle demand from the potentiometer as usual. When it exceeds the hold torque, blend from hold to throttle over a short time and leave the Hold state, so there is no dip or surge in torque. Show the state with RGB colours, for example blue for Hold and amber for Fade. Log duty and state during handover and check the duty trace is continuous.

    Checkpoint: Logged duty shows no dip or jump at handover.

  8. 8

    Tests

    Test grades from 0 to 15% using blocks, and for each grade measure modelled rollback distance with and without hill hold. Also test different handover timings, re-applying the brake during the hold, and letting the hold time out. Log pitch, state, modelled speed and duty at 100 Hz. Present a table of rollback distance by grade, and check the fade and timeout timing against the requirements.

    Checkpoint: A test table showing rollback distance with and without hill hold for each grade.

  9. 9

    Report

    Write up the physics of the hold force, the grade estimation, the state machine, the handover logic and the test results. Discuss the limits of accelerometer-only grade sensing, such as errors from vehicle acceleration and mounting angle, and how a real system would add wheel-speed sensing to detect real rollback. Include the rollback table and plots of a typical hold and handover.

    Checkpoint: Report and demonstration.

Testing & validation

  • Grade accuracy using blocks of known height
  • Modelled rollback with and without hill hold
  • Hold timeout and fade behaviour
  • Smooth handover from hold to throttle

Expected results

A hill-hold assist that estimates grade from the accelerometer, prevents modelled rollback on slopes up to the tested grade, times out safely and hands over smoothly to the rider's throttle.

Take it further

  • Fuse barometric altitude change from the BMP280 to estimate grade while moving
  • Add an encoder motor to detect real rollback and close the loop
  • Combine with an electronic parking function using the relay

Viva and review questions

Why latch pitch only at standstill?

During acceleration the accelerometer also measures vehicle acceleration, which would be mistaken for slope.

How is hold torque calculated?

Force is mass times g times sine of slope angle, multiplied by wheel radius and divided by gear ratio to get motor torque.

Why time out the hold?

Holding indefinitely would heat the motor and hide that the rider has not acted; a timeout with warning returns control to the brake.

Related EV projects

All 100 EV projects