1 | /*
|
---|
2 | * サーボ・ハード関連
|
---|
3 | *
|
---|
4 | * Copyright (c) 2016 Wakayama.rb Ruby Board developers
|
---|
5 | *
|
---|
6 | * This software is released under the MIT License.
|
---|
7 | * https://github.com/wakayamarb/wrbb-v2lib-firm/blob/master/MITL
|
---|
8 | *
|
---|
9 | */
|
---|
10 | #include <arduino.h>
|
---|
11 | /*#include <servo.h>*/
|
---|
12 |
|
---|
13 | #include <mruby.h>
|
---|
14 | /*#include <mruby/string.h>*/
|
---|
15 | /*#include <mruby/variable.h>*/
|
---|
16 |
|
---|
17 | #include "../llbruby.h"
|
---|
18 | #include "sKernel.h"
|
---|
19 |
|
---|
20 | #define ATTACH_MAX 12
|
---|
21 | struct pin_node *servo[ATTACH_MAX];
|
---|
22 |
|
---|
23 | /******************************************************/
|
---|
24 | /* サーボ出力を任意のピンに割り当てます: Servo.attach*/
|
---|
25 | /* Servo.attach(ch, pin[,min,max])*/
|
---|
26 | /* ch: サーボのチャネル 0~11まで指定できます*/
|
---|
27 | /* pin: 割り当てるピン番号*/
|
---|
28 | /* min: サーボの角度が0度のときのパルス幅(マイクロ秒)。デフォルトは544*/
|
---|
29 | /* max: サーボの角度が180度のときのパルス幅(マイクロ秒)。デフォルトは2400*/
|
---|
30 | /******************************************************/
|
---|
31 | mrb_value mrb_servo_attach(mrb_state *mrb, mrb_value self)
|
---|
32 | {
|
---|
33 | int ch;
|
---|
34 | int pinno;
|
---|
35 | int min;
|
---|
36 | int max;
|
---|
37 | struct pin_node *pin;
|
---|
38 |
|
---|
39 | int n = mrb_get_args(mrb, "ii|ii", &ch, &pinno, &min, &max);
|
---|
40 |
|
---|
41 | if (pinno >= 20) {
|
---|
42 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* nが2以下のときは、minが引数に無いので、 */
|
---|
46 | if (n <= 2) {
|
---|
47 | min = 544;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* nが3以下のときは、maxが引数に無いので、 */
|
---|
51 | if (n <= 3) {
|
---|
52 | max = 2400;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
56 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (servo[ch] != 0) {
|
---|
60 | pwmout_free(&servo[ch]->pwmout);
|
---|
61 | /*servo[ch] = 0;*/
|
---|
62 | }
|
---|
63 |
|
---|
64 | pin = get_pin(pintype_pwmout, pinno);
|
---|
65 | if (pin == NULL)
|
---|
66 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
67 |
|
---|
68 | servo[ch] = pin;
|
---|
69 | servo[ch]->min = min;
|
---|
70 | servo[ch]->max = max;
|
---|
71 |
|
---|
72 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
73 | }
|
---|
74 |
|
---|
75 | /******************************************************/
|
---|
76 | /* サーボの角度をセットします: Servo.write*/
|
---|
77 | /* Servo.write(ch,angle)*/
|
---|
78 | /* ch: サーボのチャネル 0~11まで指定できます*/
|
---|
79 | /* angle: 角度 0~180*/
|
---|
80 | /******************************************************/
|
---|
81 | mrb_value mrb_servo_write(mrb_state *mrb, mrb_value self)
|
---|
82 | {
|
---|
83 | int ch;
|
---|
84 | int angle;
|
---|
85 | int min;
|
---|
86 | int max;
|
---|
87 |
|
---|
88 | mrb_get_args(mrb, "ii", &ch, &angle);
|
---|
89 |
|
---|
90 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
91 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (servo[ch] == NULL) {
|
---|
95 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
96 | }
|
---|
97 |
|
---|
98 | DEBUG_PRINT("Servo", angle);
|
---|
99 |
|
---|
100 | min = servo[ch]->min;
|
---|
101 | max = servo[ch]->max;
|
---|
102 | angle *= servo[ch]->period;
|
---|
103 |
|
---|
104 | pwmout_pulsewidth_us(&servo[ch]->pwmout, (angle * (max - min)) / 180 + min);
|
---|
105 |
|
---|
106 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
107 | }
|
---|
108 |
|
---|
109 | /******************************************************/
|
---|
110 | /* サーボモータにus単位で角度を指定する: Servo.us*/
|
---|
111 | /* Servo.us(ch,us)*/
|
---|
112 | /* ch: サーボのチャネル 0~11まで指定できます*/
|
---|
113 | /* us: 出力したいパルスの幅 1~19999, 0で出力 OFF*/
|
---|
114 | /* サーボモータに与えられるパルスは20ms周期で、1周期中のHighの時間を直接指定する。*/
|
---|
115 | /* 実質的にPWM出力。連続回転タイプのサーボでは、回転のスピードが設定することができる。*/
|
---|
116 | /******************************************************/
|
---|
117 | mrb_value mrb_servo_us(mrb_state *mrb, mrb_value self)
|
---|
118 | {
|
---|
119 | int ch;
|
---|
120 | int us;
|
---|
121 |
|
---|
122 | mrb_get_args(mrb, "ii", &ch, &us);
|
---|
123 |
|
---|
124 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
125 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (servo[ch] == 0) {
|
---|
129 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
130 | }
|
---|
131 |
|
---|
132 | pwmout_pulsewidth_us(&servo[ch]->pwmout, us);
|
---|
133 |
|
---|
134 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
135 | }
|
---|
136 |
|
---|
137 | /******************************************************/
|
---|
138 | /* 最後に設定された角度を読み出す: Servo.read*/
|
---|
139 | /* Servo.read(ch)*/
|
---|
140 | /* ch: サーボのチャネル 0~11まで指定できます*/
|
---|
141 | /* 戻り値*/
|
---|
142 | /* マイクロ秒単位 us(ch) で与えた値は読みとれない*/
|
---|
143 | /******************************************************/
|
---|
144 | mrb_value mrb_servo_read(mrb_state *mrb, mrb_value self)
|
---|
145 | {
|
---|
146 | int ch;
|
---|
147 | int ret = 0;
|
---|
148 |
|
---|
149 | mrb_get_args(mrb, "i", &ch);
|
---|
150 |
|
---|
151 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
152 | return mrb_fixnum_value(ret);
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (servo[ch] == 0) {
|
---|
156 | return mrb_fixnum_value(ret);
|
---|
157 | }
|
---|
158 |
|
---|
159 | ret = servo[ch]->pulsewidth;
|
---|
160 |
|
---|
161 | return mrb_fixnum_value(ret);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /******************************************************/
|
---|
165 | /* ピンにサーボが割り当てられているかを確認する: Servo.attached*/
|
---|
166 | /* Servo.attached(ch)*/
|
---|
167 | /* ch: サーボのチャネル 0~11まで指定できます*/
|
---|
168 | /* 戻り値*/
|
---|
169 | /* 1: 割り当てられている*/
|
---|
170 | /* 0: 割り当てはない*/
|
---|
171 | /******************************************************/
|
---|
172 | mrb_value mrb_servo_attached(mrb_state *mrb, mrb_value self)
|
---|
173 | {
|
---|
174 | int ch;
|
---|
175 | int ret = 0;
|
---|
176 |
|
---|
177 | mrb_get_args(mrb, "i", &ch);
|
---|
178 |
|
---|
179 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
180 | return mrb_fixnum_value(ret);
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (servo[ch] == 0) {
|
---|
184 | return mrb_fixnum_value(ret);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (get_pin_type(servo[ch]->pin) == pintype_pwmout) {
|
---|
188 | ret = 1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return mrb_fixnum_value(ret);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /******************************************************/
|
---|
195 | /* サーボの動作を止め、割り込みを禁止する: Servo.detach*/
|
---|
196 | /* Servo.detach(ch)*/
|
---|
197 | /* ch: サーボのチャネル 0~9まで指定できます*/
|
---|
198 | /******************************************************/
|
---|
199 | mrb_value mrb_servo_detach(mrb_state *mrb, mrb_value self)
|
---|
200 | {
|
---|
201 | int ch;
|
---|
202 |
|
---|
203 | mrb_get_args(mrb, "i", &ch);
|
---|
204 |
|
---|
205 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
206 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (servo[ch] == NULL) {
|
---|
210 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
211 | }
|
---|
212 |
|
---|
213 | pwmout_free(&servo[ch]->pwmout);
|
---|
214 | servo[ch] = 0;
|
---|
215 |
|
---|
216 | return mrb_nil_value(); /*戻り値は無しですよ。*/
|
---|
217 | }
|
---|
218 |
|
---|
219 | /******************************************************/
|
---|
220 | /* ライブラリを定義します*/
|
---|
221 | /******************************************************/
|
---|
222 | void servo_Init(mrb_state *mrb)
|
---|
223 | {
|
---|
224 | for (int i = 0; i < ATTACH_MAX; i++) {
|
---|
225 | servo[i] = 0;
|
---|
226 | }
|
---|
227 |
|
---|
228 | struct RClass *servoModule = mrb_define_module(mrb, "Servo");
|
---|
229 |
|
---|
230 | mrb_define_module_function(mrb, servoModule, "attach", mrb_servo_attach, MRB_ARGS_REQ(2) | MRB_ARGS_OPT(2));
|
---|
231 | mrb_define_module_function(mrb, servoModule, "write", mrb_servo_write, MRB_ARGS_REQ(2));
|
---|
232 | mrb_define_module_function(mrb, servoModule, "us", mrb_servo_us, MRB_ARGS_REQ(2));
|
---|
233 | mrb_define_module_function(mrb, servoModule, "read", mrb_servo_read, MRB_ARGS_REQ(1));
|
---|
234 | mrb_define_module_function(mrb, servoModule, "attached", mrb_servo_attached, MRB_ARGS_REQ(1));
|
---|
235 | mrb_define_module_function(mrb, servoModule, "detach", mrb_servo_detach, MRB_ARGS_REQ(1));
|
---|
236 | }
|
---|