Code Monkey home page Code Monkey logo

kalman-estimator's Introduction

kalman-estimator - a Kalman estimator in C++

Contents

Example usage

// Prepare variables...

// Create Kalman estimator:

kalman estim(
    dt          // time step
    , A         // system dynamics matrix: state-k-1 => state-k
    , B         // control input matrix: control => state
    , H         // measurement output matrix: state => measurement
    , Q         // process noise covariance
    , R         // measurement noise covariance
    , P         // initial estimate error covariance
    , xhat      // initial system state estimate
);

// Use the estimator, feed it a constant acceleration, system noise and a noisy measurement:

for ( int i = 0; i*dt < T; ++i )
{
    // Use a constant commanded acceleration of 1 [m/s^2]:
    const kalman::u_t u = {1};

    // Simulate the linear system:
    x = A * x + B * u + process_noise( dt, accelnoise );

    // Simulate the noisy measurement:
    auto z = H * x + meas_noise( measnoise );

    // Process a time-step:
    estim.update( u, z );
}

In a nutshell

Try and gain familiarity with the Kalman estimator. The intention is to implement a control system using a Kalman estimator in C++ on a small computer board like the Adafruit Pro Trinket and control the positioning of a spring–mass system with it.

Plan:

Relative Performance F [kHz] Type Kalman gain Optimization Code size [B]
640 1600 Blink LED   -O2 174
           
0.18 0.473 double updating -Os 4,968
0.2 0.548 double updating -O2 5,492
0.8 2.1 double fix on %chg -Os 4,966
1.4 3.6 double fix on %chg -O2 5,490
0.7 1.7 fixed_point<int32_t> updating -Os 2.848
1 2.5 fixed_point<int32_t> updating -O2 2.490
4 9.9 fixed_point<int32_t> fix on %chg -Os 2.848
18 44.9 fixed_point<int32_t> fix on %chg -O2 2.490

Table 1. Relative performance for numeric type, fixing Kalman gain and compiler optimization, without ADC and DAC conversions.

Basic Kalman estimator code

void update( u_t const & u, z_t const & z )
{
	// --------------------------------------
	// 1. Predict (time update)
	
	// 1a: Project the state ahead:
	xhat = A * xhat + B * u;
	
	// 1b: Project the error covariance ahead:
	P = A * P * transposed(A) + Q;
	
	// --------------------------------------
	// 2. Correct (measurement update)
	
	// 2a: Compute the Kalman gain:
	K = P * transposed(H) * inverted(H * P * transposed(H) + R);
	
	// 2b: Update estimate with measurement:
	xhat = xhat + K * (z - H * xhat);
	
	// 2c: Update the error covariance:
	P = (I() - K * H) * P;
}

A constant accelerated object met system and measurement noise

Graph for a simulation run with the resulting (estimated) position, (estimated) velocity, errors and Kalman gains.

Developing and testing the software

The software is developed using both AVR-GCC and a separate C++17 compiler on a personal computer. The software is developed and tested as a PC program using the lest test framework. In parallel, the AVR-GCC compiler is used to verify that what we develop as a C++ program is acceptable as an AVR program. The tool avrdude bundled with AVR-GCC is used to upload the program to the Pro Trinket.

Prerequisites

In what follows, it is expected that AVR GCC is available. If you want to compile and run the tests, a C++17 compiler such as GNU C++ or Visual C++ 2017 is needed.

(to be continued.)

Notes and References

Contents

Kalman Estimator

Various articles, video's, books to read up on the Kalman estimator.

[1] Wikipedia. Kalman filter.
[2] Greg Welch and Gary Bishop. An Introduction to the Kalman Filter (PDF).
[3] Greg Welch. Page on The Kalman Filter.
[4] Simon D. Levy. The Extended Kalman Filter: An Interactive Tutorial for Non-Experts.
[5] Bilgin Esme. Kalman Filter For Dummies - A mathematically challenged man's search for scientific wisdom.
[6] Mayitzin. Kalman Filter – A painless approach.
[7] Tucker McClure. How Kalman Filters Work, part 1, part 2, part 3 (Code on MathWorks).
[8] Tucker McClure. How Simulations Work.
[9] Demofox. Incremental Least Squares Curve Fitting (Contains C++ code).
[10] Kristian Sloth Lauszus. A practical approach to Kalman filter and how to implement it (Contains C code).
[11] Dan Simon. Optimal State Estimation: Kalman, H-infinity, and Nonlinear Approaches (Contains Matlab code from the book).
[12] iLectureOnline. Lectures in The Kalman Filter (42 videos of 6 minutes).

Fixed point

[13] Wikipedia. Fixed-point arithmetic.
[14] Rick Regan. Number of Bits in a Decimal Integer.

Matlab

[15] MathWorks. MATLAB for Deep Learning.
[16] GNU. GNU Octave - Scientific Programming Language (largely compatible with Matlab).

C++

[17] ISOCPP. Standard C++ Foundation.
[18] CppReference. The complete online reference for the C and C++ languages and standard libraries.
[19] Martin Moene. lest test framework.

GNUC

[20] GNUC. GNUC AVR Options.
[21] AVR-GCC. AVR-GCC 8.1.0 for Windows 32 and 64 bit. Contains section Upgrading the Arduino IDE.
[22] AVRDUDE. AVR Downloader/UploaDEr.

AVR

[23] Elliot Williams. AVR Programming - Learning to Write Software for Hardware (Code).
[24] Elliot Williams. Embed with Elliot: There is no Arduino "Language".

Atmel

[25] Atmel. Atmel Studio 7.
[26] Visual Micro. Arduino for Atmel Studio 7 (plug-in). [27] Atmel. Datasheet of ATmega328 Microcontroller (PDF).

Adafruit

[28] Adafruit. Pro Trinket.
[29] Adafruit. Introducing Pro Trinket.

kalman-estimator's People

Contributors

martinmoene avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kalman-estimator's Issues

Create atmega328 abstraction

MicroChip. ATmega328/P AVR MCU with picoPower Technology Data Sheet. 2018. (PDF)

Registers

  • 11. AVR CPU Core
  • 11.3.1 SREG: Status Register
  • 11.4 r0..r31: General Purpose Register File
  • 11.4.1 The X-register, Y-register, and Z-register
  • 11.5 Stack Pointer
     
  • 12. AVR Memories
  • 12.6.2 EEARH, EEARL: EEPROM Address Register Low and High Byte
  • 12.6.3 EEDR : EEPROM Data Register
  • 12.6.4 EECR : EEPROM Control Register
  • 12.6.5 GPIOR2: General Purpose I/O Register 2
  • 12.6.6 GPIOR1: General Purpose I/O Register 1
  • 12.6.7 GPIOR0: General Purpose I/O Register 0
     
  • 13. System Clock and Clock Options
  • 13.12.1 OSCCAL: Oscillator Calibration Register
  • 13.12.2 CLKPR : Clock Prescaler Register
     
  • 14. Power Management and Sleep Modes
  • 14.12.1 SMCR : Sleep Mode Control Register
  • 14.12.2 MCUCR: MCU Control Register
  • 14.12.3 PRR : Power Reduction Register
     
  • 15. System Control and Reset
  • 15.9.1 MCUSR : MCU Status Register
  • 15.9.2 WDTCSR: Watchdog Timer Control Register
     
  • 16. Interrupts
  • 16.2.1 Moving Interrupts Between Application and Boot Space
  • 16.2.2 MCUCR: MCU Control Register
     
  • 17. EXTINT - External Interrupts
  • 17.2.1 EICRA : External Interrupt Control Register A
  • 17.2.2 EIMSK : External Interrupt Mask Register
  • 17.2.3 EIFR : External Interrupt Flag Register
  • 17.2.4 PCICR : Pin Change Interrupt Control Register
  • 17.2.5 PCIFR : Pin Change Interrupt Flag Register
  • 17.2.6 PCMSK2: Pin Change Mask Register 2
  • 17.2.7 PCMSK1: Pin Change Mask Register 1
  • 17.2.8 PCMSK0: Pin Change Mask Register 0
     
  • 18. I/O-Ports
  • 18.4.1 MCUCR: MCU Control Register
  • 18.4.2 PORTB: Port B Data Register
  • 18.4.3 DDRB : Port B Data Direction Register
  • 18.4.4 PINB : Port B Input Pins Address
  • 18.4.5 PORTC: Port C Data Register
  • 18.4.6 DDRC : Port C Data Direction Register
  • 18.4.7 PINC : Port C Input Pins Address
  • 18.4.8 PORTD: Port D Data Register
  • 18.4.9 DDRD : Port D Data Direction Register
  • 18.4.10 PIND : Port D Input Pins Address
     
  • 19. 8-bit Timer/Counter0 (TC0) with PWM
  • 19.9.1 TCCR0A: TC0 Control Register A
  • 19.9.2 TCCR0B: TC0 Control Register B
  • 19.9.3 TIMSK0: TC0 Interrupt Mask Register
  • 19.9.4 GTCCR : General Timer/Counter Control Register
  • 19.9.5 TCNT0 : TC0 Counter Value Register
  • 19.9.6 OCR0A : TC0 Output Compare Register A
  • 19.9.7 OCR0B : TC0 Output Compare Register B
  • 19.9.8 TIFR0 : TC0 Interrupt Flag Register
     
  • 20. 16-bit Timer/Counter1 (TC1) with PWM
  • 20.15.1 TCCR0A: TC1 Control Register A
  • 20.15.2 TCCR0B: TC1 Control Register B
  • 20.15.3 TCCR0C: TC1 Control Register C
  • 20.15.4 TCNT1L, TCNT1H: TC1 Counter Value Low and High byte
  • 20.15.5 ICR1L , ICR1H: Input Capture Register 1 Low and High byte
  • 20.15.6 OCR1AL, OCR1AH: Output Compare Register 1 A Low and High byte
  • 20.15.7 OCR1BL, OCR1BH: Output Compare Register 1 B Low and High byte
  • 20.15.8 TIMSK1: Timer/Counter 1 Interrupt Mask Register
  • 20.15.9 TIFR1 : TC1 Interrupt Flag Register
     
  • 21. Timer/Counter 0, 1 Prescalers
  • 21.4.1 General Timer/Counter Control Register
     
  • 22. 8-bit Timer/Counter2 (TC2) with PWM and Asynchronous Operation
  • 22.11.1 TCCR2A: TC2 Control Register A
  • 22.11.2 TCCR2B: TC2 Control Register B
  • 22.11.3 TCNT2 : TC2 Counter Value Register
  • 22.11.4 OCR2A : TC2 Output Compare Register A
  • 22.11.5 OCR2B : TC2 Output Compare Register B
  • 22.11.6 TIMSK2: TC2 Interrupt Mask Register
  • 22.11.7 TIFR2 : TC2 Interrupt Flag Register
  • 22.11.8 ASSR : Asynchronous Status Register
  • 22.11.9 GTCCR : General Timer/Counter Control Register
     
  • 23. Serial Peripheral Interface (SPI)
  • 23.5.1 SPCR0: SPI Control Register 0
  • 23.5.2 SPSR0: SPI Status Register 0
  • 23.5.3 SPDR0: SPI Data Register 0
     
  • 24. Universal Synchronous Asynchronous Receiver Transceiver (USART)
  • 24.12.1 UDR0: USART I/O Data Register 0
  • 24.12.2 UCSR0A: USART Control and Status Register 0 A
  • 24.12.3 UCSR0B: USART Control and Status Register 0 B
  • 24.12.4 UCSR0C: USART Control and Status Register 0 C
  • 24.12.5 UBRR0L, UBRR0H: USART Baud Rate 0 Register Low and High byte
     
  • 25. USART in SPI (USARTSPI) Mode
  • See 24
     
  • 26. Two-Wire Serial Interface (TWI)
  • 26.9.1 TWBR : TWI Bit Rate Register
  • 26.9.2 TWSR : TWI Status Register
  • 26.9.3 TWAR : TWI (Slave) Address Register
  • 26.9.4 TWDR : TWI Data Register
  • 26.9.5 TWCR : TWI Control Register
  • 26.9.6 TWAMR: TWI (Slave) Address Mask Register
     
  • 27. Analog Comparator (AC)
  • 27.3.1 ACSR : Analog Comparator Control and Status Register
  • 27.3.2 DIDR1: Digital Input Disable Register 1
     
  • 28. Analog-to-Digital Converter (ADC)
  • 28.9.1 ADMUX : ADC Multiplexer Selection Register
  • 28.9.2 ADCSRA: ADC Control and Status Register A
  • 28.9.3 ADCL, ADCH: ADC Data Register Low and High Byte (ADLAR=0)
  • 28.9.4 ADCL, ADCH: ADC Data Register Low and High Byte (ADLAR=1)
  • 28.9.5 ADCSRB: ADC Control and Status Register B
  • 28.9.6 DIDR0 : Digital Input Disable Register 0
     
  • 29. debugWIRE On-chip Debug System
  • 29.6.1 DWDR: debugWire Data Register
     
  • 30. Boot Loader Support – Read-While-Write Self-programming (BTLDR)
  • 30.9.1 SPMCSR: Store Program Memory Control and Status Register

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.