Rev 724 | Rev 726 | 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]; |
||
725 | giacomo | 134 | volatile unsigned int RX_position[4] = {0,0,0,0}; |
135 | volatile unsigned int TX_position[4] = {0,0,0,0}; |
||
136 | volatile unsigned int RX_cycle = 0; |
||
137 | volatile 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 | |||
725 | giacomo | 196 | int i = 0, old, oldcycle, RX; |
721 | giacomo | 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 | |||
725 | giacomo | 239 | RX = (int)RX_position; |
240 | |||
721 | giacomo | 241 | kern_frestore(f); |
242 | |||
717 | giacomo | 243 | com_irq_send(port, len_msg, msg); |
244 | |||
725 | giacomo | 245 | return RX; |
246 | |||
247 | } |
||
248 | |||
249 | int servo_wait(unsigned port, int RX, int number) { |
||
250 | |||
251 | int f = RX, i = 0; |
||
252 | |||
253 | while (i < number) { |
||
254 | f++; |
||
255 | if (f >= RXTX_BUFF_MAX) f = 0; |
||
256 | i++; |
||
257 | } |
||
258 | |||
259 | /* Active wait until number bytes received */ |
||
260 | while (RX_position[port] < f); |
||
261 | |||
286 | giacomo | 262 | return 0; |
263 | |||
264 | } |
||
265 | |||
358 | giacomo | 266 | int servo_open(int port,int speed) |
286 | giacomo | 267 | { |
268 | int err; |
||
269 | |||
358 | giacomo | 270 | err = com_open((unsigned)(port), (DWORD)speed, SERVO_PARITY, SERVO_LEN, SERVO_STOP); |
271 | |||
717 | giacomo | 272 | com_init_irq((unsigned)(port)); |
273 | |||
274 | com_set_functions(servo_confirm,servo_indication); |
||
275 | |||
721 | giacomo | 276 | com_irq_enable((unsigned)(port),ALL_IRQ); |
277 | |||
286 | giacomo | 278 | return err; |
279 | |||
280 | } |
||
281 | |||
358 | giacomo | 282 | int servo_close(int port) |
286 | giacomo | 283 | { |
284 | int err; |
||
285 | |||
724 | giacomo | 286 | com_irq_disable((unsigned)(port),ALL_IRQ); |
287 | |||
717 | giacomo | 288 | com_close_irq((unsigned)(port)); |
289 | |||
358 | giacomo | 290 | err = com_close((unsigned)(port)); |
286 | giacomo | 291 | |
292 | return err; |
||
293 | |||
294 | } |
||
295 | |||
296 | /* 1000.011w:bbbb.bbbb */ |
||
358 | giacomo | 297 | int servo_set_RS232_baudrate(int port, int baud) |
281 | giacomo | 298 | { |
724 | giacomo | 299 | unsigned char b[2]; |
300 | int spbrg_temp, i; |
||
286 | giacomo | 301 | unsigned char spbrg, w; |
358 | giacomo | 302 | int servo_port = (unsigned)(port); |
286 | giacomo | 303 | |
304 | i = 0; |
||
305 | while(BaudTable[i] != baud && BaudTable[i] != -1) i++; |
||
306 | if (BaudTable[i] == -1) { |
||
307 | kern_printf("SERVO:Error wrong baud rate\n"); |
||
308 | return -1; |
||
309 | } |
||
310 | |||
358 | giacomo | 311 | w = 1; |
453 | giacomo | 312 | spbrg_temp = (SERVO_CLOCK*10 / (16*baud)) - 10; |
412 | giacomo | 313 | if (spbrg_temp>2550) { |
358 | giacomo | 314 | w = 0; |
453 | giacomo | 315 | spbrg_temp = (SERVO_CLOCK*10 / (64*baud)) - 10; |
286 | giacomo | 316 | } |
412 | giacomo | 317 | spbrg = spbrg_temp / 10; |
453 | giacomo | 318 | if (spbrg_temp % 10 > 5) spbrg++; |
412 | giacomo | 319 | |
286 | giacomo | 320 | #ifdef SERVO_DEBUG |
321 | kern_printf("(SERVO:SBPRG %d W %d)",spbrg,w); |
||
358 | giacomo | 322 | #endif |
323 | |||
724 | giacomo | 324 | b[0] = 0x86 | (w & 0x01); |
325 | b[1] = spbrg; |
||
326 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 327 | |
724 | giacomo | 328 | return 0; |
381 | giacomo | 329 | |
281 | giacomo | 330 | } |
331 | |||
285 | giacomo | 332 | /* 1000.0101 */ |
358 | giacomo | 333 | int servo_get_RS232_baudrate(int port) |
281 | giacomo | 334 | { |
724 | giacomo | 335 | unsigned char b[1],r[2]; |
336 | int res, res_w, res_b; |
||
358 | giacomo | 337 | int servo_port = (unsigned)(port); |
725 | giacomo | 338 | int RX; |
358 | giacomo | 339 | |
724 | giacomo | 340 | b[0] = 0x85; |
725 | giacomo | 341 | RX = servo_send_msg(servo_port, b, 1, r, 2); |
358 | giacomo | 342 | |
725 | giacomo | 343 | servo_wait(servo_port, RX, 2); |
344 | |||
724 | giacomo | 345 | res_w = r[0]; /* bit W */ |
346 | res_b = r[1]; /* byte SPBRG */ |
||
465 | giacomo | 347 | |
348 | if (res_w != -1 && res_b != -1) { |
||
349 | if (res_w) |
||
350 | res = SERVO_CLOCK / ( 16 * (res_b + 1) ); |
||
351 | else |
||
352 | res = SERVO_CLOCK / ( 64 * (res_b + 1) ); |
||
353 | } else { |
||
354 | return -1; |
||
355 | } |
||
358 | giacomo | 356 | |
724 | giacomo | 357 | return res; |
358 | giacomo | 358 | |
281 | giacomo | 359 | } |
360 | |||
286 | giacomo | 361 | /* 1000.0100 */ |
358 | giacomo | 362 | int servo_store_RS232_baudrate(int port) |
281 | giacomo | 363 | { |
724 | giacomo | 364 | unsigned char b[1]; |
358 | giacomo | 365 | int servo_port = (unsigned)(port); |
366 | |||
724 | giacomo | 367 | b[0] = 0x84; |
368 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
369 | |||
370 | return 0; |
||
358 | giacomo | 371 | |
281 | giacomo | 372 | } |
373 | |||
289 | giacomo | 374 | /* 1000.1010:llll.llll */ |
358 | giacomo | 375 | int servo_set_period(int port, int period) |
281 | giacomo | 376 | { |
724 | giacomo | 377 | unsigned char b[2]; |
358 | giacomo | 378 | int servo_port = (unsigned)(port); |
379 | |||
724 | giacomo | 380 | b[0] = 0x8A; |
381 | b[1] = (period*1000)/TICK_LEN_PERIOD/8 & 0xFF; |
||
382 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 383 | |
724 | giacomo | 384 | return 0; |
358 | giacomo | 385 | |
281 | giacomo | 386 | } |
387 | |||
285 | giacomo | 388 | /* 1000.1001 */ |
358 | giacomo | 389 | int servo_get_period(int port) |
281 | giacomo | 390 | { |
724 | giacomo | 391 | unsigned char b[1],r[1]; |
725 | giacomo | 392 | int res, RX; |
358 | giacomo | 393 | int servo_port = (unsigned)(port); |
394 | |||
724 | giacomo | 395 | b[0] = 0x89; |
725 | giacomo | 396 | RX = servo_send_msg(servo_port, b, 1, r, 1); |
358 | giacomo | 397 | |
725 | giacomo | 398 | servo_wait(servo_port, RX, 1); |
399 | |||
724 | giacomo | 400 | res = r[0]; |
381 | giacomo | 401 | |
724 | giacomo | 402 | return (((unsigned char)(res))*TICK_LEN_PERIOD/1000*8); |
358 | giacomo | 403 | |
281 | giacomo | 404 | } |
405 | |||
289 | giacomo | 406 | /* 1000.1000 */ |
358 | giacomo | 407 | int servo_store_period(int port) |
281 | giacomo | 408 | { |
724 | giacomo | 409 | unsigned char b[1]; |
358 | giacomo | 410 | int servo_port = (unsigned)(port); |
411 | |||
724 | giacomo | 412 | b[0] = 0x88; |
413 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
358 | giacomo | 414 | |
724 | giacomo | 415 | return 0; |
381 | giacomo | 416 | |
281 | giacomo | 417 | } |
418 | |||
283 | giacomo | 419 | /* 1000.1100 */ |
358 | giacomo | 420 | int servo_get_setup_switch(int port) |
281 | giacomo | 421 | { |
724 | giacomo | 422 | unsigned char b[1],r[1]; |
725 | giacomo | 423 | int res, RX; |
358 | giacomo | 424 | int servo_port = (unsigned)(port); |
425 | |||
724 | giacomo | 426 | b[0] = 0x8C; |
725 | giacomo | 427 | RX = servo_send_msg(servo_port, b, 1, r, 1); |
281 | giacomo | 428 | |
725 | giacomo | 429 | servo_wait(servo_port, RX, 1); |
430 | |||
724 | giacomo | 431 | res = r[0]; |
281 | giacomo | 432 | |
724 | giacomo | 433 | return res; |
283 | giacomo | 434 | |
281 | giacomo | 435 | } |
436 | |||
283 | giacomo | 437 | /* 1000.111s */ |
358 | giacomo | 438 | int servo_set_RC5_switch(int port, int data) |
281 | giacomo | 439 | { |
724 | giacomo | 440 | unsigned char b[1]; |
358 | giacomo | 441 | int servo_port = (unsigned)(port); |
442 | |||
724 | giacomo | 443 | b[0] = 0x8E | (data & 0x01); |
444 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
281 | giacomo | 445 | |
724 | giacomo | 446 | return 0; |
381 | giacomo | 447 | |
281 | giacomo | 448 | } |
449 | |||
323 | giacomo | 450 | /* 1000.0000:0000.Mmmm */ |
358 | giacomo | 451 | int servo_turn_off(int port, int servo) |
285 | giacomo | 452 | { |
724 | giacomo | 453 | unsigned char b[2]; |
358 | giacomo | 454 | int servo_port = (unsigned)(port); |
455 | |||
456 | if (servo > 15) return -1; |
||
724 | giacomo | 457 | |
458 | b[0] = 0x80; |
||
459 | b[1] = 0x00 | (servo & 0x0F); |
||
460 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 461 | |
724 | giacomo | 462 | return 0; |
285 | giacomo | 463 | |
464 | } |
||
465 | |||
325 | giacomo | 466 | /* 1000.0000:0001.Mmmm */ |
358 | giacomo | 467 | int servo_turn_on(int port, int servo) |
285 | giacomo | 468 | { |
721 | giacomo | 469 | unsigned char b[2]; |
358 | giacomo | 470 | int servo_port = (unsigned)(port); |
471 | |||
323 | giacomo | 472 | if (servo > 15) return -1; |
358 | giacomo | 473 | |
721 | giacomo | 474 | b[0] = 0x80; |
475 | b[1] = 0x10 | (servo & 0x0F); |
||
476 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 477 | |
721 | giacomo | 478 | return 0; |
358 | giacomo | 479 | |
285 | giacomo | 480 | } |
481 | |||
291 | giacomo | 482 | /* 1000.0000:0010.0000 */ |
358 | giacomo | 483 | int servo_turn_off_all(int port) |
323 | giacomo | 484 | { |
724 | giacomo | 485 | unsigned char b[2]; |
358 | giacomo | 486 | int servo_port = (unsigned)(port); |
487 | |||
724 | giacomo | 488 | b[0] = 0x80; |
489 | b[1] = 0x20; |
||
490 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
358 | giacomo | 491 | |
724 | giacomo | 492 | return 0; |
358 | giacomo | 493 | |
285 | giacomo | 494 | } |
495 | |||
291 | giacomo | 496 | /* 1000.0000:0010.0001 */ |
358 | giacomo | 497 | int servo_turn_on_all(int port) |
285 | giacomo | 498 | { |
724 | giacomo | 499 | unsigned char b[2]; |
358 | giacomo | 500 | int servo_port = (unsigned)(port); |
501 | |||
724 | giacomo | 502 | b[0] = 0x80; |
503 | b[1] = 0x21; |
||
504 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
505 | |||
506 | return 0; |
||
358 | giacomo | 507 | |
285 | giacomo | 508 | } |
509 | |||
323 | giacomo | 510 | /* 1000.0000:0101.000M:mmmm.mmmm */ |
724 | giacomo | 511 | int servo_set_levels(int port, int bank, int mask) |
315 | giacomo | 512 | { |
724 | giacomo | 513 | unsigned char b[3]; |
358 | giacomo | 514 | int servo_port = (unsigned)(port); |
515 | |||
724 | giacomo | 516 | b[0] = 0x80; |
517 | b[1] = 0x50 | (0x01 & bank); |
||
518 | b[2] = (unsigned char)(mask & 0xFF); |
||
519 | servo_send_msg(servo_port, b, 3, NULL, 0); |
||
358 | giacomo | 520 | |
724 | giacomo | 521 | return 0; |
358 | giacomo | 522 | |
315 | giacomo | 523 | } |
524 | |||
323 | giacomo | 525 | /* 1000.0000:0100.000M */ |
358 | giacomo | 526 | int servo_get_levels(int port, int bank) |
297 | giacomo | 527 | { |
724 | giacomo | 528 | unsigned char b[2],r[1]; |
725 | giacomo | 529 | int res, RX; |
358 | giacomo | 530 | int servo_port = (unsigned)(port); |
531 | |||
724 | giacomo | 532 | b[0] = 0x80; |
533 | b[1] = 0x40 | (0x01 & bank); |
||
725 | giacomo | 534 | RX = servo_send_msg(servo_port, b, 2, r, 1); |
381 | giacomo | 535 | |
725 | giacomo | 536 | servo_wait(servo_port, RX, 1); |
537 | |||
724 | giacomo | 538 | res = r[0]; |
358 | giacomo | 539 | |
724 | giacomo | 540 | return res; |
358 | giacomo | 541 | |
297 | giacomo | 542 | } |
543 | |||
544 | /* 1000.0000:1000.0000 */ |
||
358 | giacomo | 545 | int servo_store_levels(int port) |
297 | giacomo | 546 | { |
724 | giacomo | 547 | unsigned char b[2]; |
358 | giacomo | 548 | int servo_port = (unsigned)(port); |
549 | |||
724 | giacomo | 550 | b[0] = 0x80; |
551 | b[1] = 0x80; |
||
552 | servo_send_msg(servo_port, b, 2, NULL, 0); |
||
381 | giacomo | 553 | |
724 | giacomo | 554 | return 0; |
358 | giacomo | 555 | |
297 | giacomo | 556 | } |
557 | |||
493 | giacomo | 558 | int servo_set_max_angle_sec(int port, int servo, int angle_sec) |
290 | giacomo | 559 | { |
560 | |||
381 | giacomo | 561 | servo_table[port][servo].max_angle_sec = angle_sec; |
290 | giacomo | 562 | return 0; |
563 | |||
564 | } |
||
565 | |||
493 | giacomo | 566 | int servo_set_min_angle_sec(int port, int servo, int angle_sec) |
290 | giacomo | 567 | { |
568 | |||
381 | giacomo | 569 | servo_table[port][servo].min_angle_sec = angle_sec; |
290 | giacomo | 570 | return 0; |
571 | |||
572 | } |
||
573 | |||
381 | giacomo | 574 | int servo_set_servo_tick(int port, int servo, int zero_tick, int delta_tick) |
575 | { |
||
576 | |||
577 | if (zero_tick != -1) servo_table[port][servo].zero_tick = zero_tick; |
||
578 | if (delta_tick != -1) servo_table[port][servo].delta_tick = delta_tick; |
||
579 | return 0; |
||
580 | |||
581 | } |
||
582 | |||
323 | giacomo | 583 | /* 0000.Pppp:0000.vvvv:vvvv.vvvv */ |
358 | giacomo | 584 | int servo_set_angle_sec(int port, int servo, int angle_sec) |
281 | giacomo | 585 | { |
717 | giacomo | 586 | unsigned char b[3]; |
587 | int angle_tick; |
||
358 | giacomo | 588 | int servo_port = (unsigned)(port); |
282 | giacomo | 589 | |
323 | giacomo | 590 | if (servo > 15) return -1; |
282 | giacomo | 591 | |
381 | giacomo | 592 | angle_tick = (servo_table[port][servo].zero_tick + angle_sec * |
593 | servo_table[port][servo].delta_tick / |
||
542 | giacomo | 594 | (servo_table[port][servo].max_angle_sec - servo_table[port][servo].min_angle_sec)) * 1000 / TICK_LEN; |
290 | giacomo | 595 | |
717 | giacomo | 596 | b[0] = 0x00 | (servo & 0x0F); |
597 | b[1] = 0x00 | ((angle_tick >> 8) & 0x0F); |
||
598 | b[2] = 0x00 | (angle_tick & 0xFF); |
||
599 | servo_send_msg(servo_port, b, 3, NULL, 0); |
||
358 | giacomo | 600 | |
717 | giacomo | 601 | return 0; |
282 | giacomo | 602 | |
281 | giacomo | 603 | } |
604 | |||
323 | giacomo | 605 | /* 0010.Pppp */ |
358 | giacomo | 606 | int servo_store_default_position(int port, int servo) |
290 | giacomo | 607 | { |
724 | giacomo | 608 | unsigned char b[1]; |
358 | giacomo | 609 | int servo_port = (unsigned)(port); |
290 | giacomo | 610 | |
323 | giacomo | 611 | if (servo > 15) return -1; |
358 | giacomo | 612 | |
724 | giacomo | 613 | b[0] = 0x20 | (servo & 0x0F); |
614 | servo_send_msg(servo_port, b, 1, NULL, 0); |
||
358 | giacomo | 615 | |
724 | giacomo | 616 | return 0; |
358 | giacomo | 617 | |
290 | giacomo | 618 | } |
619 | |||
323 | giacomo | 620 | /* 0001.Pppp */ |
358 | giacomo | 621 | int servo_get_angle_sec(int port, int servo) |
281 | giacomo | 622 | { |
724 | giacomo | 623 | unsigned char b[1],r[2]; |
725 | giacomo | 624 | int res,data,RX; |
358 | giacomo | 625 | int servo_port = (unsigned)(port); |
626 | |||
323 | giacomo | 627 | if (servo > 15) return -1; |
358 | giacomo | 628 | |
724 | giacomo | 629 | b[0] = 0x10 | (servo & 0x0F); |
725 | giacomo | 630 | RX = servo_send_msg(servo_port, b, 1, r, 2); |
281 | giacomo | 631 | |
725 | giacomo | 632 | servo_wait(servo_port, RX, 2); |
633 | |||
724 | giacomo | 634 | res = r[0] << 8; |
635 | res |= r[1]; |
||
282 | giacomo | 636 | |
381 | giacomo | 637 | data = ((res*TICK_LEN/1000) - servo_table[port][servo].zero_tick) * |
638 | (servo_table[port][servo].max_angle_sec - servo_table[port][servo].min_angle_sec) / |
||
639 | servo_table[port][servo].delta_tick; |
||
290 | giacomo | 640 | |
724 | giacomo | 641 | return data; |
282 | giacomo | 642 | |
281 | giacomo | 643 | } |
644 | |||
282 | giacomo | 645 | /* 0100:0aaa */ |
358 | giacomo | 646 | int servo_get_analog(int port, int adport) |
281 | giacomo | 647 | { |
724 | giacomo | 648 | unsigned char b[1],r[2]; |
725 | giacomo | 649 | int res, RX; |
358 | giacomo | 650 | int servo_port = (unsigned)(port); |
651 | |||
381 | giacomo | 652 | if (adport > 7) return -1; |
358 | giacomo | 653 | |
724 | giacomo | 654 | b[0] = 0x40 | (adport & 0x07); |
725 | giacomo | 655 | RX = servo_send_msg(servo_port, b, 1, r, 2); |
281 | giacomo | 656 | |
725 | giacomo | 657 | servo_wait(servo_port, RX, 2); |
658 | |||
724 | giacomo | 659 | res = r[0] << 8; |
660 | res |= r[1]; |
||
282 | giacomo | 661 | |
724 | giacomo | 662 | return res; |
282 | giacomo | 663 | |
281 | giacomo | 664 | } |
665 |