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