Updates along with SDK1.3.0 release (#181)
Bug fixes and new examples Co-authored-by: Paulo Marques <pm@quant-insight.com> Co-authored-by: martin <admin@crossleys.biz> Co-authored-by: matiasilva <matias.silva@raspberrypi.com> Co-authored-by: Uri Shaked <uri@urishaked.com> Co-authored-by: Diego Solano <diegosolano@gmail.com> Co-authored-by: Andrew Scheller <andrew.scheller@raspberrypi.com> Co-authored-by: Adrian Hesketh <a-h@users.noreply.github.com> Co-authored-by: Emircan Gündoğdu <58917386+emircangun@users.noreply.github.com> Co-authored-by: Josef Wegner <80200012+josefwegner@users.noreply.github.com> Co-authored-by: pmarques-dev <72901351+pmarques-dev@users.noreply.github.com> Co-authored-by: Paulo Marques <pm@quant-insight.com> Co-authored-by: mjcross <mjcross@users.noreply.github.com> Co-authored-by: martin <admin@crossleys.biz>
This commit is contained in:
61
pio/quadrature_encoder/quadrature_encoder.c
Normal file
61
pio/quadrature_encoder/quadrature_encoder.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2021 pmarques-dev @ github
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/pio.h"
|
||||
#include "hardware/timer.h"
|
||||
|
||||
#include "quadrature_encoder.pio.h"
|
||||
|
||||
//
|
||||
// ---- quadrature encoder interface example
|
||||
//
|
||||
// the PIO program reads phase A/B of a quadrature encoder and increments or
|
||||
// decrements an internal counter to keep the current absolute step count
|
||||
// updated. At any point, the main code can query the current count by using
|
||||
// the quadrature_encoder_*_count functions. The counter is kept in a full
|
||||
// 32 bit register that just wraps around. Two's complement arithmetic means
|
||||
// that it can be interpreted as a 32-bit signed or unsigned value, and it will
|
||||
// work anyway.
|
||||
//
|
||||
// As an example, a two wheel robot being controlled at 100Hz, can use two
|
||||
// state machines to read the two encoders and in the main control loop it can
|
||||
// simply ask for the current encoder counts to get the absolute step count. It
|
||||
// can also subtract the values from the last sample to check how many steps
|
||||
// each wheel as done since the last sample period.
|
||||
//
|
||||
// One advantage of this approach is that it requires zero CPU time to keep the
|
||||
// encoder count updated and because of that it supports very high step rates.
|
||||
//
|
||||
|
||||
int main() {
|
||||
int new_value, delta, old_value = 0;
|
||||
|
||||
// Base pin to connect the A phase of the encoder.
|
||||
// The B phase must be connected to the next pin
|
||||
const uint PIN_AB = 10;
|
||||
|
||||
stdio_init_all();
|
||||
|
||||
PIO pio = pio0;
|
||||
const uint sm = 0;
|
||||
|
||||
uint offset = pio_add_program(pio, &quadrature_encoder_program);
|
||||
quadrature_encoder_program_init(pio, sm, offset, PIN_AB, 0);
|
||||
|
||||
while (1) {
|
||||
// note: thanks to two's complement arithmetic delta will always
|
||||
// be correct even when new_value wraps around MAXINT / MININT
|
||||
new_value = quadrature_encoder_get_count(pio, sm);
|
||||
delta = new_value - old_value;
|
||||
old_value = new_value;
|
||||
|
||||
printf("position %8d, delta %6d\n", new_value, delta);
|
||||
sleep_ms(100);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user