Overview

PyPi module N/A
git repository https://bitbucket.org/arrizza-public/bresenham-tweak
git command git clone git@bitbucket.org:arrizza-public/bresenham-tweak.git
Verification Report https://arrizza.com/web-ver/bresenham-tweak-report.html
Version Info
OS Language #Runs Last Run Cov%
Ubuntu 24.04 noble Python 3.10 917 2025-01-21 100.0%

Summary

This C++ project shows the use of Bresenham's algorithm to convert a series of pulses from a real number line to an integer number line.

For example, this can be used to pulse a stepper motor (or a DC motor) accurately based on the current RTC value. The rate the pulses occur will be a real number and the expected time to pulse will therefore also be a real number. But to do this accurately with minimal error in a discrete/integer scenario will need to convert the current time, the expected pulses to deliver at that moment, etc.

The Bresenham algorithm does this for convert a line (real values) to pixels on the screen. This project is to tweak that algorithm a little to make it work easier in this situation.

See doc/derivation.md for a detailed explanation of how this derived from the full Bresenham algorithm.

Assumptions

The line represents fluid delivery. The fluid is delivered at a rate equivalent to the slope of the line starting from 0,0 continuously from there.

  • line starts at the cartesian origin: 0,0.
  • the rate is equivalent to slope of the y = mx + b (b is 0)
  • the rate is positive i.e. the delivery is in the upper right quadrant in the cartesian plane.
  • the rate is never perfectly vertical. Can't deliver infinite fluid.
  • the call to get_y() is done precisely on the x-axis. The first time x=1, next x=2, etc.
  • used float instead of double so that the code can be used on older Arduinos

Run

see sample/main.cpp for an example

  • The slope is set by calling init(slope) or init_xy(x,y).
  • Then the get_() function periodically e.g. every mS or 10 mS or every second.
  • The returned y value is the amount fluid required at that point.

It is up to the caller to convert the fluid needed into motor rotation or pulses to send to the stepper or whatever mechanism is needed by the fluid pump.

./doit

Typical output:

 --  1] (1,0)  <== line goes from 0,0 to (10, 2)
 --  2] (2,0)
 --  3] (3,1)
 --  4] (4,1)
 --  5] (5,1)
 --  6] (6,1)
 --  7] (7,1)
 --  8] (8,2)
 --  9] (9,2)
 -- 10] (10,2)
 -- 11] ---- done
 -- 12] (1,0)  <== same line but defined by the use of a slope of 0.2 i.e. 2/10
 -- 13] (2,0)
 -- 14] (3,1)
 -- 15] (4,1)
 -- 16] (5,1)
 -- 17] (6,1)
 -- 18] (7,1)
 -- 19] (8,2)
 -- 20] (9,2)
 -- 21] (10,2)
 -- 22] ---- done

- John Arrizza