adinput.c
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <math.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <time.h>
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36 
37 /* ボディパラメータ */
38 #include <shvel-param.h>
39 
40 /* yp-spur用 */
41 #include <serial.h>
42 #include <odometry.h>
43 #include <utility.h>
44 
45 /* ライブラリ用 */
46 #include <ypspur.h>
47 
48 int ad[16]; // ad値保存用バッファ
49 int ad_num = 0; // adでいくつデータを出力するように指令したか
50 int dio_num = 0; // dioでいくつデータを出力するように指令したか
51 
52 int process_addata(unsigned char *buf, int len)
53 {
54  int j;
55  int ad_num;
56 
57  ad_num = 0;
58  for (j = 0; j < len; j += 2)
59  {
60  Short_2Char data;
61  data.byte[1] = buf[j];
62  data.byte[0] = buf[j + 1];
63  ad[((unsigned short)data.integer) >> 12] = (unsigned short)data.integer & 0x0FFF;
64  // printf("%d=%d\n",((unsigned short)data.integer) >> 12,(unsigned short)data.integer & 0x0FFF);
65  ad_num++;
66  }
67  return ad_num;
68 }
69 
70 /* read ad data func for ssm write */
71 const int *get_addataptr()
72 {
73  return ad;
74 }
75 
76 int get_addata(int num)
77 {
78  if (num < 0)
79  return 0;
80  if (num > 15)
81  return 0;
82  return ad[num];
83 }
84 
85 int get_ad_num(void)
86 {
87  return ad_num;
88 }
89 
90 int get_dio_num(void)
91 {
92  return dio_num;
93 }
94 
95 int admask_receive(char *buf, int len, double receive_time, void *data)
96 {
97  buf[len] = 0;
98  strcat((char *)data, buf);
99  if (strstr((char *)data, "00P\n\n"))
100  return -1;
101  if (strstr((char *)data, "\n\n"))
102  return -2;
103  return 0;
104 }
105 
106 int set_admask(unsigned char mask)
107 {
108  /* Send & Recive Buffer */
109  char buf[2048];
110  int i;
111 
112  ad_num = 0;
113  strcpy(buf, "\n\n\n\n");
114  serial_write(buf, strlen(buf));
115  yp_usleep(50000);
116  serial_flush_in();
117  yp_usleep(50000);
118  strcpy(buf, "ADMASK");
119  for (i = 0; i < 8; i++)
120  {
121  if (mask & 0x80)
122  {
123  ad_num++;
124  strcat(buf, "1");
125  }
126  else
127  strcat(buf, "0");
128  mask = mask << 1;
129  }
130  strcat(buf, "\n");
131  serial_write(buf, strlen(buf));
132 
133  buf[0] = 0;
134  if (serial_recieve(admask_receive, buf) == -1)
135  return 1;
136 
137  return 0;
138 }
139 
140 int set_diomask(unsigned char enable)
141 {
142  /* Send & Recive Buffer */
143  char buf[2048];
144  dio_num = 0;
145 
146  strcpy(buf, "\n\n\n\n");
147  serial_write(buf, strlen(buf));
148  yp_usleep(50000);
149  serial_flush_in();
150  yp_usleep(50000);
151  strcpy(buf, "GETIO");
152  if (enable)
153  {
154  strcat(buf, "1");
155  dio_num++;
156  }
157  else
158  strcat(buf, "0");
159  strcat(buf, "\n");
160  serial_write(buf, strlen(buf));
161 
162  buf[0] = 0;
163  if (serial_recieve(admask_receive, buf) == -1)
164  return 1;
165 
166  return 0;
167 }
int serial_write(char *buf, int len)
Definition: serial.c:539
void yp_usleep(int usec)
Definition: utility.c:54
int ad_num
Definition: adinput.c:49
int get_ad_num(void)
Definition: adinput.c:85
short integer
Definition: shvel-param.h:32
char byte[2]
Definition: shvel-param.h:33
int ad[16]
Definition: adinput.c:48
int set_diomask(unsigned char enable)
Definition: adinput.c:140
int get_dio_num(void)
Definition: adinput.c:90
int dio_num
Definition: adinput.c:50
int serial_recieve(int(*serial_event)(char *, int, double, void *), void *data)
Definition: serial.c:431
int get_addata(int num)
Definition: adinput.c:76
int set_admask(unsigned char mask)
Definition: adinput.c:106
int process_addata(unsigned char *buf, int len)
Definition: adinput.c:52
int admask_receive(char *buf, int len, double receive_time, void *data)
Definition: adinput.c:95
void serial_flush_in(void)
Definition: serial.c:385
const int * get_addataptr()
Definition: adinput.c:71


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24