forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dda_maths.h
66 lines (52 loc) · 2.31 KB
/
dda_maths.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _DDA_MATHS_H
#define _DDA_MATHS_H
#include <stdint.h>
#include "config.h"
// return rounded result of multiplicand * multiplier / divisor
// this version is with quotient and remainder precalculated elsewhere
const int32_t muldivQR(int32_t multiplicand, uint32_t qn, uint32_t rn,
uint32_t divisor);
// return rounded result of multiplicand * multiplier / divisor
static int32_t muldiv(int32_t, uint32_t, uint32_t) __attribute__ ((always_inline));
inline int32_t muldiv(int32_t multiplicand, uint32_t multiplier,
uint32_t divisor) {
return muldivQR(multiplicand, multiplier / divisor,
multiplier % divisor, divisor);
}
/*
micrometer distance <=> motor step distance conversions
*/
// Like shown in the patch attached to this post:
// http://forums.reprap.org/read.php?147,89710,130225#msg-130225 ,
// it might be worth pre-calculating muldivQR()'s qn and rn in dda_init()
// as soon as STEPS_PER_M_{XYZE} is no longer a compile-time variable.
static int32_t um_to_steps_x(int32_t) __attribute__ ((always_inline));
inline int32_t um_to_steps_x(int32_t distance) {
return muldivQR(distance, STEPS_PER_M_X / 1000000UL,
STEPS_PER_M_X % 1000000UL, 1000000UL);
}
static int32_t um_to_steps_y(int32_t) __attribute__ ((always_inline));
inline int32_t um_to_steps_y(int32_t distance) {
return muldivQR(distance, STEPS_PER_M_Y / 1000000UL,
STEPS_PER_M_Y % 1000000UL, 1000000UL);
}
static int32_t um_to_steps_z(int32_t) __attribute__ ((always_inline));
inline int32_t um_to_steps_z(int32_t distance) {
return muldivQR(distance, STEPS_PER_M_Z / 1000000UL,
STEPS_PER_M_Z % 1000000UL, 1000000UL);
}
static int32_t um_to_steps_e(int32_t) __attribute__ ((always_inline));
inline int32_t um_to_steps_e(int32_t distance) {
return muldivQR(distance, STEPS_PER_M_E / 1000000UL,
STEPS_PER_M_E % 1000000UL, 1000000UL);
}
// approximate 2D distance
uint32_t approx_distance(uint32_t dx, uint32_t dy);
// approximate 3D distance
uint32_t approx_distance_3(uint32_t dx, uint32_t dy, uint32_t dz);
// integer square root algorithm
uint16_t int_sqrt(uint32_t a);
// this is an ultra-crude pseudo-logarithm routine, such that:
// 2 ^ msbloc(v) >= v
const uint8_t msbloc (uint32_t v);
#endif /* _DDA_MATHS_H */