Rev 721 | Rev 725 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
281 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
381 | giacomo | 10 | * Mauro Marinoni <mauro.marinoni@unipv.it> |
281 | giacomo | 11 | * (see the web pages for full authors list) |
12 | * |
||
13 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
14 | * |
||
15 | * http://www.sssup.it |
||
16 | * http://retis.sssup.it |
||
17 | * http://shark.sssup.it |
||
18 | */ |
||
19 | |||
20 | /* |
||
21 | * Copyright (C) 2002 Paolo Gai |
||
22 | * |
||
23 | * This program is free software; you can redistribute it and/or modify |
||
24 | * it under the terms of the GNU General Public License as published by |
||
25 | * the Free Software Foundation; either version 2 of the License, or |
||
26 | * (at your option) any later version. |
||
27 | * |
||
28 | * This program is distributed in the hope that it will be useful, |
||
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
31 | * GNU General Public License for more details. |
||
32 | * |
||
33 | * You should have received a copy of the GNU General Public License |
||
34 | * along with this program; if not, write to the Free Software |
||
35 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
36 | * |
||
37 | */ |
||
38 | |||
282 | giacomo | 39 | #include "kernel/kern.h" |
281 | giacomo | 40 | |
717 | giacomo | 41 | #include "drivers/scom.h" |
42 | #include "drivers/scomirq.h" |
||
281 | giacomo | 43 | |
717 | giacomo | 44 | #include "servo.h" |
286 | giacomo | 45 | |
358 | giacomo | 46 | //#define SERVO_DEBUG |
286 | giacomo | 47 | |
398 | giacomo | 48 | #define SERVO_SPEED 19200 |
286 | giacomo | 49 | #define SERVO_PARITY NONE |
50 | #define SERVO_LEN 8 |
||
51 | #define SERVO_STOP 1 |
||
282 | giacomo | 52 | |
358 | giacomo | 53 | #define SERVO_CLOCK 20000000 /* 20MHz */ |
54 | |||
315 | giacomo | 55 | #define TICK_LEN 1600 /* ns */ |
316 | giacomo | 56 | #define TICK_LEN_PERIOD 51200 /* ns */ |
290 | giacomo | 57 | |
58 | struct servo_data { |
||
59 | int min_angle_sec; |
||
60 | int max_angle_sec; |
||
61 | int delta_tick; |
||
62 | int zero_tick; |
||
63 | }; |
||
64 | |||
381 | giacomo | 65 | struct servo_data servo_table[4][16] = { |
66 | {{-324000, 324000, 1200, 1600}, |
||
316 | giacomo | 67 | {-324000, 324000, 1200, 1600}, |
68 | {-324000, 324000, 1200, 1600}, |
||
69 | {-324000, 324000, 1200, 1600}, |
||
70 | {-324000, 324000, 1200, 1600}, |
||
71 | {-324000, 324000, 1200, 1600}, |
||
72 | {-324000, 324000, 1200, 1600}, |
||
73 | {-324000, 324000, 1200, 1600}, |
||
335 | giacomo | 74 | {-324000, 324000, 1200, 1600}, |
75 | {-324000, 324000, 1200, 1600}, |
||
76 | {-324000, 324000, 1200, 1600}, |
||
77 | {-324000, 324000, 1200, 1600}, |
||
78 | {-324000, 324000, 1200, 1600}, |
||
79 | {-324000, 324000, 1200, 1600}, |
||
80 | {-324000, 324000, 1200, 1600}, |
||
381 | giacomo | 81 | {-324000, 324000, 1200, 1600}}, |
82 | {{-324000, 324000, 1200, 1600}, |
||
335 | giacomo | 83 | {-324000, 324000, 1200, 1600}, |
381 | giacomo | 84 | {-324000, 324000, 1200, 1600}, |
85 | {-324000, 324000, 1200, 1600}, |
||
86 | {-324000, 324000, 1200, 1600}, |
||
87 | {-324000, 324000, 1200, 1600}, |
||
88 | {-324000, 324000, 1200, 1600}, |
||
89 | {-324000, 324000, 1200, 1600}, |
||
90 | {-324000, 324000, 1200, 1600}, |
||
91 | {-324000, 324000, 1200, 1600}, |
||
92 | {-324000, 324000, 1200, 1600}, |
||
93 | {-324000, 324000, 1200, 1600}, |
||
94 | {-324000, 324000, 1200, 1600}, |
||
95 | {-324000, 324000, 1200, 1600}, |
||
96 | {-324000, 324000, 1200, 1600}, |
||
97 | {-324000, 324000, 1200, 1600}}, |
||
98 | {{-324000, 324000, 1200, 1600}, |
||
99 | {-324000, 324000, 1200, 1600}, |
||
100 | {-324000, 324000, 1200, 1600}, |
||
101 | {-324000, 324000, 1200, 1600}, |
||
102 | {-324000, 324000, 1200, 1600}, |
||
103 | {-324000, 324000, 1200, 1600}, |
||
104 | {-324000, 324000, 1200, 1600}, |
||
105 | {-324000, 324000, 1200, 1600}, |
||
106 | {-324000, 324000, 1200, 1600}, |
||
107 | {-324000, 324000, 1200, 1600}, |
||
108 | {-324000, 324000, 1200, 1600}, |
||
109 | {-324000, 324000, 1200, 1600}, |
||
110 | {-324000, 324000, 1200, 1600}, |
||
111 | {-324000, 324000, 1200, 1600}, |
||
112 | {-324000, 324000, 1200, 1600}, |
||
113 | {-324000, 324000, 1200, 1600}}, |
||
114 | {{-324000, 324000, 1200, 1600}, |
||
115 | {-324000, 324000, 1200, 1600}, |
||
116 | {-324000, 324000, 1200, 1600}, |
||
117 | {-324000, 324000, 1200, 1600}, |
||
118 | {-324000, 324000, 1200, 1600}, |
||
119 | {-324000, 324000, 1200, 1600}, |
||
120 | {-324000, 324000, 1200, 1600}, |
||
121 | {-324000, 324000, 1200, 1600}, |
||
122 | {-324000, 324000, 1200, 1600}, |
||
123 | {-324000, 324000, 1200, 1600}, |
||
124 | {-324000, 324000, 1200, 1600}, |
||
125 | {-324000, 324000, 1200, 1600}, |
||
126 | {-324000, 324000, 1200, 1600}, |
||
127 | {-324000, 324000, 1200, 1600}, |
||
128 | {-324000, 324000, 1200, 1600}, |
||
129 | {-324000, 324000, 1200, 1600}}}; |
||
290 | giacomo | 130 | |
717 | giacomo | 131 | #define RXTX_BUFF_MAX 100 |
132 | static BYTE RXTX_buff[4][RXTX_BUFF_MAX]; |
||
133 | static BYTE *RXTX_addr[4][RXTX_BUFF_MAX]; |
||
134 | unsigned int RX_position[4] = {0,0,0,0}; |
||
135 | unsigned int TX_position[4] = {0,0,0,0}; |
||
721 | giacomo | 136 | unsigned int RX_cycle = 0; |
137 | unsigned int TX_cycle = 0; |
||
286 | giacomo | 138 | |
139 | const int BaudTable[] = { |
||
140 | 1200, |
||
141 | 2400, |
||
142 | 4800, |
||
143 | 9600, |
||
144 | 14400, |
||
145 | 19200, |
||
146 | 38400, |
||
147 | 57600, |
||
148 | 115200, |
||
149 | -1}; |
||
150 | |||
721 | giacomo | 151 | void servo_rx_error(unsigned port, unsigned type) { |
292 | giacomo | 152 | |
721 | giacomo | 153 | kern_printf("(SERVO: BUFFER ERROR PORT:%d ERR:%d)",port,type); |
717 | giacomo | 154 | |
281 | giacomo | 155 | } |
156 | |||
717 | giacomo | 157 | void servo_indication(unsigned port, BYTE data) { |
721 | giacomo | 158 | |
717 | giacomo | 159 | if (RXTX_addr[port][RX_position[port]] == NULL) { |
160 | if (data != RXTX_buff[port][RX_position[port]]) |
||
721 | giacomo | 161 | servo_rx_error(port,1); |
717 | giacomo | 162 | RX_position[port]++; |
721 | giacomo | 163 | |
164 | if (RX_position[port] >= RXTX_BUFF_MAX) { |
||
165 | RX_cycle++; |
||
166 | RX_position[port] = 0; |
||
167 | } |
||
168 | if ((RX_cycle == TX_cycle && RX_position[port] > TX_position[port]) || |
||
169 | (RX_cycle > TX_cycle)) |
||
170 | servo_rx_error(port,2); |
||
171 | |||
717 | giacomo | 172 | } else { |
173 | *RXTX_addr[port][RX_position[port]] = data; |
||
174 | RX_position[port]++; |
||
721 | giacomo | 175 | |
176 | if (RX_position[port] >= RXTX_BUFF_MAX) { |
||
177 | RX_cycle++; |
||
178 | RX_position[port] = 0; |
||
179 | } |
||
180 | if ((RX_cycle == TX_cycle && RX_position[port] > TX_position[port]) || |
||
181 | (RX_cycle > TX_cycle)) |
||
182 | servo_rx_error(port,2); |
||
717 | giacomo | 183 | } |
295 | giacomo | 184 | |
286 | giacomo | 185 | } |
186 | |||
717 | giacomo | 187 | void servo_confirm(unsigned port, BYTE msg_status) { |
286 | giacomo | 188 | |
717 | giacomo | 189 | if (msg_status == COM_ERROR) |
721 | giacomo | 190 | kern_printf("(SERVO: PORT:%d ERROR)",port); |
286 | giacomo | 191 | |
717 | giacomo | 192 | } |
193 | |||
194 | int servo_send_msg(unsigned port, BYTE *msg, unsigned len_msg, BYTE *res, unsigned len_res) { |
||
195 | |||
721 | giacomo | 196 | int i = 0, old, oldcycle; |
197 | SYS_FLAGS f; |
||
198 | |||
199 | f = kern_fsave(); |
||
200 | |||
717 | giacomo | 201 | old = TX_position[port]; |
721 | giacomo | 202 | oldcycle = TX_cycle; |
717 | giacomo | 203 | |
204 | while(i < len_msg) { |
||
205 | RXTX_buff[port][TX_position[port]] = msg[i]; |
||
206 | RXTX_addr[port][TX_position[port]] = NULL; |
||
207 | TX_position[port]++; |
||
721 | giacomo | 208 | if (TX_position[port] >= RXTX_BUFF_MAX) { |
209 | TX_cycle++; |
||
210 | TX_position[port] = 0; |
||
211 | } |
||
717 | giacomo | 212 | if (TX_position[port] == RX_position[port]) { |
213 | TX_position[port] = old; |
||
721 | giacomo | 214 | TX_cycle = oldcycle; |
215 | kern_frestore(f); |
||
717 | giacomo | 216 | return -1; |
217 | } |
||
218 | i++; |
||
219 | } |
||
220 | |||
221 | i = 0; |
||
222 | while(i < len_res) { |
||
223 | RXTX_buff[port][TX_position[port]] = 0; |
||
224 | RXTX_addr[port][TX_position[port]] = res+i; |
||
225 | TX_position[port]++; |
||
721 | giacomo | 226 | if (TX_position[port] >= RXTX_BUFF_MAX) { |
227 | TX_cycle++; |
||
228 | TX_position[port] = 0; |
||
229 | } |
||
717 | giacomo | 230 | if (TX_position[port] == RX_position[port]) { |
231 | TX_position[port] = old; |
||
721 | giacomo | 232 | TX_cycle = oldcycle; |
233 | kern_frestore(f); |
||
717 | giacomo | 234 | return -1; |
235 | } |
||
236 | i++; |
||
237 | } |
||
238 | |||
721 | giacomo | 239 | kern_frestore(f); |
240 | |||
717 | giacomo | 241 | com_irq_send(port, len_msg, msg); |
242 | |||
286 | giacomo | 243 | return 0; |
244 | |||
245 | } |
||
246 | |||
358 | giacomo | 247 | int servo_open(int port,int speed) |
286 | giacomo | 248 | { |
249 | int err; |
||
250 | |||
358 | giacomo | 251 | err = com_open((unsigned)(port), (DWORD)speed, SERVO_PARITY, SERVO_LEN, SERVO_STOP); |
252 | |||
717 | giacomo | 253 | com_init_irq((unsigned)(port)); |
254 | |||
255 | com_set_functions(servo_confirm,servo_indication); |
||
256 | |||
721 | giacomo | 257 | com_irq_enable((unsigned)(port),ALL_IRQ); |
258 | |||
286 | giacomo | 259 | return err; |
260 | |||
261 | } |
||
262 | |||
358 | giacomo | 263 | int servo_close(int port) |
286 | giacomo | 264 | { |
265 | int err; |
||
266 | |||
724 | giacomo | 267 | com_irq_disable((unsigned)(port),ALL_IRQ); |
268 | |||
717 | giacomo | 269 | com_close_irq((unsigned)(port)); |
270 | |||
358 | giacomo | 271 | err = com_close((unsigned)(port)); |
286 | giacomo | 272 | |
273 | return err; |
||
274 | |||
275 | } |
||
276 | |||
277 | /* 1000.011w:bbbb.bbbb */ |
||
358 | giacomo | 278 | int servo_set_RS232_baudrate(int port, int baud) |
281 | giacomo | 279 | { |
724 | giacomo | 280 | unsigned char b[2]; |
281 | int spbrg_temp, i; |
||
286 | giacomo | 282 | unsigned char spbrg, w; |
358 | giacomo | 283 | int servo_port = (unsigned)(port); |
286 | giacomo | 284 | |
285 | i = 0; |
||
286 | while(BaudTable[i] != baud && BaudTable[i] != -1) i++; |
||
287 | if (BaudTable[i] == -1) { |
||
288 | kern_printf("SERVO:Error wrong baud rate\n"); |
||
289 | return -1; |
||
290 | } |
||
291 | |||
358 | giacomo | 292 | w = 1; |
453 | giacomo | 293 | spbrg_temp = (SERVO_CLOCK*10 / (16*baud)) - 10; |
412 | giacomo | 294 | if (spbrg_temp>2550) { |
358 | giacomo | 295 | w = 0; |
453 | giacomo | 296 | spbrg_temp = (SERVO_CLOCK*10 / (64*baud)) - 10; |
286 | giacomo | 297 | } |
412 | giacomo | 298 | spbrg = spbrg_temp / 10; |
453 | giacomo | 299 | if (spbrg_temp % 10 > 5) spbrg++; |
412 | giacomo | 300 | |
286 | giacomo | 301 | #ifdef SERVO_DEBUG |
302 | kern_printf("(SERVO:SBPRG %d W %d)",spbrg,w); |
||
358 | giacomo | 303 | #endif |
304 | |||
724 | giacomo | 305 | b[0] = 0x86 | (w & 0x01); |
306 | b[1] = spbrg; |
||
307 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 308 | |
724 | giacomo | 309 | return 0; |
381 | giacomo | 310 | |
281 | giacomo | 311 | } |
312 | |||
285 | giacomo | 313 | /* 1000.0101 */ |
358 | giacomo | 314 | int servo_get_RS232_baudrate(int port) |
281 | giacomo | 315 | { |
724 | giacomo | 316 | unsigned char b[1],r[2]; |
317 | int res, res_w, res_b; |
||
358 | giacomo | 318 | int servo_port = (unsigned)(port); |
319 | |||
724 | giacomo | 320 | b[0] = 0x85; |
321 | servo_send_msg(servo_port, b, 1, r, 2); |
||
358 | giacomo | 322 | |
724 | giacomo | 323 | res_w = r[0]; /* bit W */ |
324 | res_b = r[1]; /* byte SPBRG */ |
||
465 | giacomo | 325 | |
326 | if (res_w != -1 && res_b != -1) { |
||
327 | if (res_w) |
||
328 | res = SERVO_CLOCK / ( 16 * (res_b + 1) ); |
||
329 | else |
||
330 | res = SERVO_CLOCK / ( 64 * (res_b + 1) ); |
||
331 | } else { |
||
332 | return -1; |
||
333 | } |
||
358 | giacomo | 334 | |
724 | giacomo | 335 | return res; |
358 | giacomo | 336 | |
281 | giacomo | 337 | } |
338 | |||
286 | giacomo | 339 | /* 1000.0100 */ |
358 | giacomo | 340 | int servo_store_RS232_baudrate(int port) |
281 | giacomo | 341 | { |
724 | giacomo | 342 | unsigned char b[1]; |
358 | giacomo | 343 | int servo_port = (unsigned)(port); |
344 | |||
724 | giacomo | 345 | b[0] = 0x84; |
346 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
347 | |||
348 | return 0; |
||
358 | giacomo | 349 | |
281 | giacomo | 350 | } |
351 | |||
289 | giacomo | 352 | /* 1000.1010:llll.llll */ |
358 | giacomo | 353 | int servo_set_period(int port, int period) |
281 | giacomo | 354 | { |
724 | giacomo | 355 | unsigned char b[2]; |
358 | giacomo | 356 | int servo_port = (unsigned)(port); |
357 | |||
724 | giacomo | 358 | b[0] = 0x8A; |
359 | b[1] = (period*1000)/TICK_LEN_PERIOD/8 & 0xFF; |
||
360 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 361 | |
724 | giacomo | 362 | return 0; |
358 | giacomo | 363 | |
281 | giacomo | 364 | } |
365 | |||
285 | giacomo | 366 | /* 1000.1001 */ |
358 | giacomo | 367 | int servo_get_period(int port) |
281 | giacomo | 368 | { |
724 | giacomo | 369 | unsigned char b[1],r[1]; |
370 | int res; |
||
358 | giacomo | 371 | int servo_port = (unsigned)(port); |
372 | |||
724 | giacomo | 373 | b[0] = 0x89; |
374 | servo_send_msg(servo_port, b, 1, r, 1); |
||
358 | giacomo | 375 | |
724 | giacomo | 376 | res = r[0]; |
381 | giacomo | 377 | |
724 | giacomo | 378 | return (((unsigned char)(res))*TICK_LEN_PERIOD/1000*8); |
358 | giacomo | 379 | |
281 | giacomo | 380 | } |
381 | |||
289 | giacomo | 382 | /* 1000.1000 */ |
358 | giacomo | 383 | int servo_store_period(int port) |
281 | giacomo | 384 | { |
724 | giacomo | 385 | unsigned char b[1]; |
358 | giacomo | 386 | int servo_port = (unsigned)(port); |
387 | |||
724 | giacomo | 388 | b[0] = 0x88; |
389 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
358 | giacomo | 390 | |
724 | giacomo | 391 | return 0; |
381 | giacomo | 392 | |
281 | giacomo | 393 | } |
394 | |||
283 | giacomo | 395 | /* 1000.1100 */ |
358 | giacomo | 396 | int servo_get_setup_switch(int port) |
281 | giacomo | 397 | { |
724 | giacomo | 398 | unsigned char b[1],r[1]; |
399 | int res; |
||
358 | giacomo | 400 | int servo_port = (unsigned)(port); |
401 | |||
724 | giacomo | 402 | b[0] = 0x8C; |
403 | servo_send_msg(servo_port, b, 1, r, 1); |
||
281 | giacomo | 404 | |
724 | giacomo | 405 | res = r[0]; |
281 | giacomo | 406 | |
724 | giacomo | 407 | return res; |
283 | giacomo | 408 | |
281 | giacomo | 409 | } |
410 | |||
283 | giacomo | 411 | /* 1000.111s */ |
358 | giacomo | 412 | int servo_set_RC5_switch(int port, int data) |
281 | giacomo | 413 | { |
724 | giacomo | 414 | unsigned char b[1]; |
358 | giacomo | 415 | int servo_port = (unsigned)(port); |
416 | |||
724 | giacomo | 417 | b[0] = 0x8E | (data & 0x01); |
418 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
281 | giacomo | 419 | |
724 | giacomo | 420 | return 0; |
381 | giacomo | 421 | |
281 | giacomo | 422 | } |
423 | |||
323 | giacomo | 424 | /* 1000.0000:0000.Mmmm */ |
358 | giacomo | 425 | int servo_turn_off(int port, int servo) |
285 | giacomo | 426 | { |
724 | giacomo | 427 | unsigned char b[2]; |
358 | giacomo | 428 | int servo_port = (unsigned)(port); |
429 | |||
430 | if (servo > 15) return -1; |
||
724 | giacomo | 431 | |
432 | b[0] = 0x80; |
||
433 | b[1] = 0x00 | (servo & 0x0F); |
||
434 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 435 | |
724 | giacomo | 436 | return 0; |
285 | giacomo | 437 | |
438 | } |
||
439 | |||
325 | giacomo | 440 | /* 1000.0000:0001.Mmmm */ |
358 | giacomo | 441 | int servo_turn_on(int port, int servo) |
285 | giacomo | 442 | { |
721 | giacomo | 443 | unsigned char b[2]; |
358 | giacomo | 444 | int servo_port = (unsigned)(port); |
445 | |||
323 | giacomo | 446 | if (servo > 15) return -1; |
358 | giacomo | 447 | |
721 | giacomo | 448 | b[0] = 0x80; |
449 | b[1] = 0x10 | (servo & 0x0F); |
||
450 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 451 | |
721 | giacomo | 452 | return 0; |
358 | giacomo | 453 | |
285 | giacomo | 454 | } |
455 | |||
291 | giacomo | 456 | /* 1000.0000:0010.0000 */ |
358 | giacomo | 457 | int servo_turn_off_all(int port) |
323 | giacomo | 458 | { |
724 | giacomo | 459 | unsigned char b[2]; |
358 | giacomo | 460 | int servo_port = (unsigned)(port); |
461 | |||
724 | giacomo | 462 | b[0] = 0x80; |
463 | b[1] = 0x20; |
||
464 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 465 | |
724 | giacomo | 466 | return 0; |
358 | giacomo | 467 | |
285 | giacomo | 468 | } |
469 | |||
291 | giacomo | 470 | /* 1000.0000:0010.0001 */ |
358 | giacomo | 471 | int servo_turn_on_all(int port) |
285 | giacomo | 472 | { |
724 | giacomo | 473 | unsigned char b[2]; |
358 | giacomo | 474 | int servo_port = (unsigned)(port); |
475 | |||
724 | giacomo | 476 | b[0] = 0x80; |
477 | b[1] = 0x21; |
||
478 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
479 | |||
480 | return 0; |
||
358 | giacomo | 481 | |
285 | giacomo | 482 | } |
483 | |||
323 | giacomo | 484 | /* 1000.0000:0101.000M:mmmm.mmmm */ |
724 | giacomo | 485 | int servo_set_levels(int port, int bank, int mask) |
315 | giacomo | 486 | { |
724 | giacomo | 487 | unsigned char b[3]; |
358 | giacomo | 488 | int servo_port = (unsigned)(port); |
489 | |||
724 | giacomo | 490 | b[0] = 0x80; |
491 | b[1] = 0x50 | (0x01 & bank); |
||
492 | b[2] = (unsigned char)(mask & 0xFF); |
||
493 | servo_send_msg(servo_port, b, 3, NULL, 0); |
||
358 | giacomo | 494 | |
724 | giacomo | 495 | return 0; |
358 | giacomo | 496 | |
315 | giacomo | 497 | } |
498 | |||
323 | giacomo | 499 | /* 1000.0000:0100.000M */ |
358 | giacomo | 500 | int servo_get_levels(int port, int bank) |
297 | giacomo | 501 | { |
724 | giacomo | 502 | unsigned char b[2],r[1]; |
503 | int res; |
||
358 | giacomo | 504 | int servo_port = (unsigned)(port); |
505 | |||
724 | giacomo | 506 | b[0] = 0x80; |
507 | b[1] = 0x40 | (0x01 & bank); |
||
508 | servo_send_msg(servo_port, b, 2, r, 1); |
||
381 | giacomo | 509 | |
724 | giacomo | 510 | res = r[0]; |
358 | giacomo | 511 | |
724 | giacomo | 512 | return res; |
358 | giacomo | 513 | |
297 | giacomo | 514 | } |
515 | |||
516 | /* 1000.0000:1000.0000 */ |
||
358 | giacomo | 517 | int servo_store_levels(int port) |
297 | giacomo | 518 | { |
724 | giacomo | 519 | unsigned char b[2]; |
358 | giacomo | 520 | int servo_port = (unsigned)(port); |
521 | |||
724 | giacomo | 522 | b[0] = 0x80; |
523 | b[1] = 0x80; |
||
524 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 525 | |
724 | giacomo | 526 | return 0; |
358 | giacomo | 527 | |
297 | giacomo | 528 | } |
529 | |||
493 | giacomo | 530 | int servo_set_max_angle_sec(int port, int servo, int angle_sec) |
290 | giacomo | 531 | { |
532 | |||
381 | giacomo | 533 | servo_table[port][servo].max_angle_sec = angle_sec; |
290 | giacomo | 534 | return 0; |
535 | |||
536 | } |
||
537 | |||
493 | giacomo | 538 | int servo_set_min_angle_sec(int port, int servo, int angle_sec) |
290 | giacomo | 539 | { |
540 | |||
381 | giacomo | 541 | servo_table[port][servo].min_angle_sec = angle_sec; |
290 | giacomo | 542 | return 0; |
543 | |||
544 | } |
||
545 | |||
381 | giacomo | 546 | int servo_set_servo_tick(int port, int servo, int zero_tick, int delta_tick) |
547 | { |
||
548 | |||
549 | if (zero_tick != -1) servo_table[port][servo].zero_tick = zero_tick; |
||
550 | if (delta_tick != -1) servo_table[port][servo].delta_tick = delta_tick; |
||
551 | return 0; |
||
552 | |||
553 | } |
||
554 | |||
323 | giacomo | 555 | /* 0000.Pppp:0000.vvvv:vvvv.vvvv */ |
358 | giacomo | 556 | int servo_set_angle_sec(int port, int servo, int angle_sec) |
281 | giacomo | 557 | { |
717 | giacomo | 558 | unsigned char b[3]; |
559 | int angle_tick; |
||
358 | giacomo | 560 | int servo_port = (unsigned)(port); |
282 | giacomo | 561 | |
323 | giacomo | 562 | if (servo > 15) return -1; |
282 | giacomo | 563 | |
381 | giacomo | 564 | angle_tick = (servo_table[port][servo].zero_tick + angle_sec * |
565 | servo_table[port][servo].delta_tick / |
||
542 | giacomo | 566 | (servo_table[port][servo].max_angle_sec - servo_table[port][servo].min_angle_sec)) * 1000 / TICK_LEN; |
290 | giacomo | 567 | |
717 | giacomo | 568 | b[0] = 0x00 | (servo & 0x0F); |
569 | b[1] = 0x00 | ((angle_tick >> 8) & 0x0F); |
||
570 | b[2] = 0x00 | (angle_tick & 0xFF); |
||
571 | servo_send_msg(servo_port, b, 3, NULL, 0); |
||
358 | giacomo | 572 | |
717 | giacomo | 573 | return 0; |
282 | giacomo | 574 | |
281 | giacomo | 575 | } |
576 | |||
323 | giacomo | 577 | /* 0010.Pppp */ |
358 | giacomo | 578 | int servo_store_default_position(int port, int servo) |
290 | giacomo | 579 | { |
724 | giacomo | 580 | unsigned char b[1]; |
358 | giacomo | 581 | int servo_port = (unsigned)(port); |
290 | giacomo | 582 | |
323 | giacomo | 583 | if (servo > 15) return -1; |
358 | giacomo | 584 | |
724 | giacomo | 585 | b[0] = 0x20 | (servo & 0x0F); |
586 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
358 | giacomo | 587 | |
724 | giacomo | 588 | return 0; |
358 | giacomo | 589 | |
290 | giacomo | 590 | } |
591 | |||
323 | giacomo | 592 | /* 0001.Pppp */ |
358 | giacomo | 593 | int servo_get_angle_sec(int port, int servo) |
281 | giacomo | 594 | { |
724 | giacomo | 595 | unsigned char b[1],r[2]; |
596 | int res,data; |
||
358 | giacomo | 597 | int servo_port = (unsigned)(port); |
598 | |||
323 | giacomo | 599 | if (servo > 15) return -1; |
358 | giacomo | 600 | |
724 | giacomo | 601 | b[0] = 0x10 | (servo & 0x0F); |
602 | servo_send_msg(servo_port, b, 1, r, 2); |
||
281 | giacomo | 603 | |
724 | giacomo | 604 | res = r[0] << 8; |
605 | res |= r[1]; |
||
282 | giacomo | 606 | |
381 | giacomo | 607 | data = ((res*TICK_LEN/1000) - servo_table[port][servo].zero_tick) * |
608 | (servo_table[port][servo].max_angle_sec - servo_table[port][servo].min_angle_sec) / |
||
609 | servo_table[port][servo].delta_tick; |
||
290 | giacomo | 610 | |
724 | giacomo | 611 | return data; |
282 | giacomo | 612 | |
281 | giacomo | 613 | } |
614 | |||
282 | giacomo | 615 | /* 0100:0aaa */ |
358 | giacomo | 616 | int servo_get_analog(int port, int adport) |
281 | giacomo | 617 | { |
724 | giacomo | 618 | unsigned char b[1],r[2]; |
619 | int res; |
||
358 | giacomo | 620 | int servo_port = (unsigned)(port); |
621 | |||
381 | giacomo | 622 | if (adport > 7) return -1; |
358 | giacomo | 623 | |
724 | giacomo | 624 | b[0] = 0x40 | (adport & 0x07); |
625 | servo_send_msg(servo_port, b, 1, r, 2); |
||
281 | giacomo | 626 | |
724 | giacomo | 627 | res = r[0] << 8; |
628 | res |= r[1]; |
||
282 | giacomo | 629 | |
724 | giacomo | 630 | return res; |
282 | giacomo | 631 | |
281 | giacomo | 632 | } |
633 |