Skip to content

Commit cc0d33c

Browse files
authored
Merge pull request #1 from irishpatrick/feature/no-more-floats
[feature] - no more floats
2 parents f608b72 + 59b267f commit cc0d33c

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "3rdparty/pico-libfixmath"]
2+
path = 3rdparty/pico-libfixmath
3+
url = https://github.com/irishpatrick/pico-libfixmath.git

3rdparty/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/pico-libfixmath)

3rdparty/pico-libfixmath

Submodule pico-libfixmath added at a450be3

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ project(pico-servo)
77
# Initialize the SDK
88
pico_sdk_init()
99

10+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty)
11+
1012
add_library(${PROJECT_NAME}
1113
src/pico_servo.c
1214
)
1315

14-
target_include_directories(${PROJECT_NAME} PRIVATE include)
16+
target_include_directories(${PROJECT_NAME} PRIVATE include ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/pico-libfixmath/include)
1517

1618
# Pull in our pico_stdlib which pulls in commonly used features
17-
target_link_libraries(${PROJECT_NAME} pico_stdlib hardware_pwm)
19+
target_link_libraries(${PROJECT_NAME} pico_stdlib hardware_pwm fixmath)
1820

1921
if (${BUILD_EXAMPLES})
20-
add_subdirectory(examples)
22+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples)
2123
endif()

examples/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
add_executable(simple simple.c)
22

3-
pico_enable_stdio_usb(simple 1)
4-
pico_enable_stdio_uart(simple 0)
3+
pico_enable_stdio_usb(simple 0)
4+
pico_enable_stdio_uart(simple 1)
55

66
# create map/bin/hex file etc.
77
pico_add_extra_outputs(simple)
88

99
target_include_directories(simple PRIVATE ../include)
1010
target_link_directories(simple PRIVATE ../build)
11-
target_link_libraries(simple PRIVATE pico_stdlib hardware_pwm pico-servo)
11+
target_link_libraries(simple PRIVATE pico_stdlib hardware_pwm pico-servo fixmath)

examples/simple.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,19 @@ int main()
7474

7575
while (1)
7676
{
77-
if (!get_bootsel_button())
78-
{
77+
if (!get_bootsel_button())
78+
{
7979
continue;
80-
}
81-
servo_move_to(A, 0);
82-
servo_move_to(B, 0);
83-
sleep_ms(500);
84-
servo_move_to(A, 180);
85-
servo_move_to(B, 90);
86-
sleep_ms(500);
80+
}
81+
else
82+
{
83+
servo_move_to(A, 0);
84+
servo_move_to(B, 0);
85+
sleep_ms(500);
86+
servo_move_to(A, 180);
87+
servo_move_to(B, 90);
88+
sleep_ms(500);
89+
}
8790
}
8891
return 0;
8992
}

src/pico_servo.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
#include "hardware/clocks.h"
3030
#include "hardware/structs/pll.h"
3131
#include "hardware/structs/clocks.h"
32+
33+
#include "fixmath.h"
34+
3235
#include <string.h>
3336

3437
#define KILO 1e3
3538
#define MICRO 1e-6
3639
#define WRAP 10000
37-
#define FREQ 50 // PWM frequency in hertz
40+
#define PWM_FREQ 50 // PWM frequency in hertz
3841

3942
static float clkdiv;
4043
static uint min;
@@ -48,7 +51,7 @@ static pwm_config slice_cfg[8];
4851

4952
static uint min_us = 500;
5053
static uint max_us = 2500;
51-
static float us_per_unit = 0.f;
54+
static fix16_t us_per_unit = 0.f;
5255

5356
static void wrap_cb(void)
5457
{
@@ -82,10 +85,10 @@ void servo_set_bounds(uint a, uint b)
8285
{
8386
min_us = a;
8487
max_us = b;
85-
if (us_per_unit > 0.f)
88+
if (fix16_to_float(us_per_unit) > 0.0f)
8689
{
8790
min = min_us / us_per_unit;
88-
max = max_us / us_per_unit;
91+
max = max_us / us_per_unit;
8992
}
9093
}
9194

@@ -105,7 +108,6 @@ int servo_init(void)
105108
memset(servo_pos, 0, 32 * sizeof(uint));
106109
memset(servo_pos_buf, 0, 16 * sizeof(uint));
107110

108-
//irq_set_exclusive_handler(PWM_IRQ_WRAP, wrap_cb);
109111
irq_add_shared_handler(PWM_IRQ_WRAP, wrap_cb, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
110112

111113
return 0;
@@ -130,12 +132,12 @@ int servo_clock_auto(void)
130132
*/
131133
int servo_clock_source(uint src)
132134
{
133-
clkdiv = (float)frequency_count_khz(src) * (float)KILO / (FREQ * WRAP);
135+
clkdiv = (float)frequency_count_khz(src) * (float)KILO / (PWM_FREQ * WRAP);
134136
if (clkdiv == 0)
135137
{
136138
return 1;
137139
}
138-
us_per_unit = 1.f / (FREQ * WRAP) / MICRO;
140+
us_per_unit = 1.f / (PWM_FREQ * WRAP) / MICRO;
139141

140142
min = min_us / us_per_unit;
141143
max = max_us / us_per_unit;
@@ -151,13 +153,13 @@ int servo_clock_source(uint src)
151153
*/
152154
int servo_clock_manual(uint freq)
153155
{
154-
clkdiv = (float)freq * 1000.f / (FREQ * WRAP);
156+
clkdiv = (float)(freq * KILO) / (float)(PWM_FREQ * WRAP);
155157
if (clkdiv == 0)
156158
{
157159
return 1;
158160
}
159-
min = 0.025f * WRAP;
160-
max = 0.125f * WRAP;
161+
min = 0.025f * (float)WRAP;
162+
max = 0.125f * (float)WRAP;
161163

162164
return 0;
163165
}
@@ -213,7 +215,13 @@ int servo_move_to(uint pin, uint angle)
213215
return 1;
214216
}
215217

216-
uint val = (float)angle / 180.f * (max - min) + min;
218+
uint val = (uint)fix16_to_int(
219+
fix16_mul(
220+
fix16_div(fix16_from_int(angle), fix16_from_int(180)),
221+
fix16_from_int(max - min)
222+
)
223+
) + min;
224+
217225
uint pos = slice_map[pin] + (pin % 2);
218226
servo_pos[16 * servo_pos_buf[pos] + pos] = val;
219227
servo_pos_buf[pos] = (servo_pos_buf[pos] + 1) % 2;

0 commit comments

Comments
 (0)