Lean4 port of Arduino balance car controller

Related tags

Miscellaneous lean4
Overview

lean4-balance-car

This is a small proof-of-concept exercise to show a Lean 4 program controlling a real robotics platform which requires low latency calculations (less the robot tip over); it has not yet involved anything more exotic like modeling physics, proving controller properties, etc.

It primarily contains a Lean 4 port of an Arduino balance car controller. Specifically, the balance car transmits sensor readings via a bluetooth serial connection to a Lean4 controller running on a Raspberry Pi 4, which replies with commands to keep the car balanced.

Architectural Overview

Here is a short video of the car balancing using this setup.

Balance Car Hardware

Yahboom Coding Robot Car Balance Robot Electronics Programmable Kit for Adult Support C Language (UNO R3 Include) https://www.amazon.com/Yahboom-Compatible-Electronics-Programmable-Education/dp/B07FL2QR1V

Important Files

  • Arduino Depencencies

    • The arduino-deps directory contains the Arduino support libraries used by the balance car code. They need to be manually installed in the location the Arduino IDE looks for such dependencies.
  • Arduino Balance Car files

    • ArduinoBalanceCar/ArduinoBalanceCar.ino contains code which when run on the Arduino will allow the car to balance. If has a debug mode (see the commented out #define DEBUG) which can be run to causes the car to (1) wait for an initial signal to begin balancing and (2) relay the sampled data and some computational results accross the serial connection to aid in debugging.
  • Arduino Lean Balance Car files

    • LeanBalanceCar/LeanBalanceCar.ino contains code meant to be run on the Arduino to communicate with an external Lean process over serial to get commands on when to drive the motors. It waits until an initial byte is received before it starts sending the 5ms interrupt-driven readings from the sensors over the serial connection.
  • Lean controller code

    • BalanceCar.lean is the lean program meant to control the balance car when it is running the aforementioned LeanBalanceCar.ino program. It depends on some C code in Serial.cpp for the low-level details of the serial port communication.
      • N.B., there is a debugMode boolean flag in BalanceCar.lean which makes the Lean executable, instead of try to communicate over a serial connection to control the car, take raw data files containing the content emitted from the ArduinoBalanceCar.ino in DEBUG mode and compares the Lean computed pwm1/pwm2 values against what the car computed.
  • C Debugging Code

    • balance-car.c is a copy-and-paste of the code in ArduinoBalanceCar.ino modified to simulate the car calculations given a file containing the raw data emitted from ArduinoBalanceCar.ino in DEBUG mode. (It should be trivial to build and run with any C compiler -- it has no local dependencies.)
  • Sampled Debug Data

    • raw-debut-data*.txt files contain sampled debug data gathered from ArduinoBalanceCar.ino in DEBUG mode.

Building the Lean4 controller

  1. Ensure elan is installed (a tool for like rustup or ghcup but for lean).

  2. Run leanmake in the root directory of the repo.

  3. If successful, the binary build/bin/balance-car should exist.

Pairing the BalanceCar with the RPi4

  • bluetoothctl
    • scan on
    • turn on car
    • pair 00:20:10:08:56:DA (code 1234)
    • trust 00:20:10:08:56:DA
  • sdptool add --channel=1 SP
  • sudo rfcomm connect hci0 00:20:10:08:56:DA
    • Reports Connected /dev/rfcomm0 to 00:20:10:08:56:DA on channel 1 and blocks until the process killed or the connection terminates.

Running the Car against the Lean Controller

  1. Ensure LeanBalanceCar/LeanBalanceCar.ino is loaded onto the Arduino.

  2. Build the Lean balance-car binary on the RPi4.

  3. Turn the Car on.

  4. On the RPi4, run sudo rfcomm connect hci0 00:20:10:08:56:DA (assuming the car has already been paired with the RPi4, as described in a previous section). This will block this particular terminal.

  5. In another terminal, run sudo ./build/bin/balance-car /dev/rfcomm0 115200 to connect the Lean4 controller to the car.

You might also like...
Dubins-Curves - Path generation for the Dubin's car
Dubins-Curves - Path generation for the Dubin's car

Dubins-Curves About This software finds the shortest paths between configurations for the Dubins' car [Dubins51], the forward only car-like vehicle wi

Documentation and code for rooting and extending a Bosch car head unit (lcn2kai)
Documentation and code for rooting and extending a Bosch car head unit (lcn2kai)

Rooting Bosch lcn2kai Headunit My Nissan Xterra came with a (for the time) modern head unit that has a touch screen, built-in navigation, backup camer

GTA SA FMOD mod, realistic car engine sounds.
GTA SA FMOD mod, realistic car engine sounds.

GTA FMOD Informations FMOD is a proprietary sound effects engine and authoring tool for video games and applications developed by Firelight Technologi

Car Whispering: the AI Mechanic TinyML Audio Event Detection

CarWhispering Car Whispering: the AI Mechanic TinyML Audio Event Detection Welcome to the AI Mechanic, an ambitious project that aims to build a globa

Convert ATARI ATR files to CAR (SWITCHABLE XEGS CARTRIDGE)

ATR2CAR Convert ATARI ATR files to CAR (SWITCHABLE XEGS CARTRIDGE) Konwerter uruchamiamy z wiersza poleceń: atr2car File.atr File.car [-c] [-128|-256|

Project uses an Arduino Leonardo to interface an A1UP Street Fighter Table controller boards with a pc.
Project uses an Arduino Leonardo to interface an A1UP Street Fighter Table controller boards with a pc.

Street Fighter Arcade1Up Table Arduino Interface Goal of this project Arcade1Up uses proprietary circuit boards to interface with the game's circuit b

Use an Arduino board as a Playstation 1 / 2 Controller.
Use an Arduino board as a Playstation 1 / 2 Controller.

Arduino PSX Controller This project is a short of example on how to use an Arduino board as a controller for the Playstation 1 or 2. Caveats This libr

4 channel servo controller code for Arduino Mega 2560 for AASD-15A and similar motor driver

eh-msc-4dof 4 channel servo controller code for Arduino Mega 2560 for AASD-15A and similar motor driver Arduino sketch for generating pulses needed to

Play Nintendo Switch using an original N64 controller via an Arduino Uno!

N64 - Arduino Uno - Nintendo Switch Description By connecting an original N64 controller to an Arduino UNO R3 running this code, and plugging the US

Owner
Galois, Inc.
Galois, Inc.
D2R mod generator. Provide quick tool to generate .txt files to change game balance: increase drop, monster density or even randomize items.

Diablo 2 mod generator Generator is inspired by d2modmaker. It provides fast and easy way to create mod without any modding knowledge. Features includ

Smirnov Vladimir 31 Dec 22, 2022
Arduino GPS Car Tracking with GPRS/HTTP

Arduino GPS Car Tracking with GPRS/HTTP this is a simple car tracking source and module to start hacking around Overview Overview DIY Module Features

TheAliBigdeli 3 Nov 26, 2021
Arduino library for controlling the movements of a 2wd robotic car using a H-bridge motor driver L298P

RoboticCar Arduino library for controlling the movements of a 2wd robotic car using a H-bridge motor driver L298P Install the library Download this re

José Augusto Cintra 1 Nov 21, 2021
A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

N.A.G.E.K.I. A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control. 中文版

NanaNana 39 Dec 8, 2022
A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

N.A.G.E.K.I. PLEASE CHECK Main Project A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Us

NanaNana 11 Dec 30, 2021
custom esp8266 controller for driving the pwm led controller

room8266 custom esp8266 controller for driving the pwm led controller designed to drive this: https://github.com/austinscreations/PWM-LED-Controller t

null 1 Nov 1, 2021
This project helps a person park their car in their garage in the same place every time.

garage-parking-sensor Description This project is developed to help a person park their car in their garage in the same place every time. Normally peo

Calvin Pereira 1 Aug 18, 2022
Self driving car with obstacle detection and avoidance

STM32F4-Self-Driving-Car-Mini-Project Self driving car with obstacle detection and avoidance Hardware STM32F401RE Dev Board HCSR04 ultrasonic sensor (

Olaoluwa Raji 2 Jan 6, 2022
Tiny and cheap robot car for inspecting sewer pipes >= 125 mm. With pan servo for the ESP32-Cam module

ESP32-Cam Sewer inspection car Version 1.0.0 - work in progress Based on esp32-cam-webserver by Owen Carter. Additional Features Pan servo for the ESP

Armin 5 Nov 6, 2022
A nonlinear MPC used to control an autonomous car.

MPC local planner A nonlinear MPC used to control an autonomous car. Description This repository contains an implementation of a nonlinear MPC that is

Tor Børve Rasmussen 11 Dec 8, 2022