UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Servo.hpp
[詳解]
1//
2// Raspberry Pi Pico用PWMサーボドライバ
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
9#if defined(ARDUINO_ARCH_RP2040)
10
11# include "pico/stdlib.h"
12# include "hardware/pwm.h"
13
14namespace Udon
15{
16
18 class Servo
19 {
20 private:
21 uint8_t pin;
22 uint slice_num;
23 uint pwmChannel;
24 uint32_t writeTime;
25
26 public:
27 void attach(uint8_t servoPin)
28 {
29 pin = servoPin;
30 pwmChannel = pin % 2 == 0 ? PWM_CHAN_A : PWM_CHAN_B;
31 gpio_set_function(pin, GPIO_FUNC_PWM);
32 slice_num = pwm_gpio_to_slice_num(pin);
33 pwm_set_clkdiv(slice_num, F_CPU / 1000000); // 分周比の設定
34 pwm_set_wrap(slice_num, 19999); // PWMの最大値
35 pwm_set_enabled(slice_num, true); // PWM有効
36 }
37 void write(uint8_t angle)
38 {
39 writeMicroseconds(map(angle, 0, 180, 500, 2500));
40 }
41 void writeMicroseconds(unsigned long time)
42 {
43 writeTime = time;
44 pwm_set_chan_level(slice_num, pwmChannel, writeTime + 1);
45 }
46 // debug
47 unsigned long readMicroseconds() const
48 {
49 return writeTime;
50 }
51 uint8_t read() const
52 {
53 return map(readMicroseconds(), 500, 2500, 0, 180);
54 }
55 };
56} // namespace Udon
57
58#endif
Definition Bit.hpp:12