class_dioc.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 
33 class_dioc::class_dioc(const char*pdevice)
34 {
35  //Get the device
36  device=(char*)malloc((strlen(pdevice)+10)*sizeof(char));
37  strcpy(device,pdevice);
38 
39  active=false;//comm off-line
40 
41  //Comm config params
42  struct termios params;
43  int ret;
44 
45  memset( &params,0, sizeof(params)); //setting all structure values to zero
46 
47  //Opens port comm with the servos
48  printf("Opening comm %s ... ",device);fflush(stdout);
49  port = open( device, O_RDWR | O_NONBLOCK ); //open com device. read write and non blocking modes
50  if(port == -1) //if could not open
51  {perror("Failed to open port");return;}
52  printf("Done\n");
53 
54  active=true;//Comm is now active
55 
56  printf("Set new parameters ... ");fflush(stdout);
57  params.c_cflag = B9600 | CS8 | CLOCAL | CREAD | IGNPAR;
58  params.c_iflag = IGNPAR;
59  params.c_oflag = 0;
60 
61  ret=tcsetattr(port, TCSANOW, &params ); // set serial communication parameters
62  if( ret < 0 )
63  {perror("Set serial communication parameters failed");return;}
64  printf("Done\n");
65 
66  return;
67 }
68 
70 {
71  if(active)
72  close(port);
73 }
74 
76 {
77  int ret=1;
78  char data;
79 
80  if(!active)
81  return;
82 
83  while(ret!=0)
84  {
85  ret=read(port,&data,1);
86  if(ret<0)
87  return;
88  }
89 }
90 
91 int class_dioc::SetStatus(id_enum ID, int STATUS)
92 {
93  char msg;
94 
95  if(STATUS)
96  msg = 0x80 | ID;
97  else
98  msg = ID;
99 
100  ret = write(port,&msg,sizeof(msg));
101  if(ret<0)
102  {
103  strcpy(err,"Cannot write to port");
104  return -3;
105  }
106 
107  return 0;
108 }
109 
111 {
112  char msg;
113 
114  if(STATUS == ON || STATUS == BLINK3)
115  msg = 0x80 | ID;
116  else
117  msg = ID;
118 
119  ret = write(port,&msg,sizeof(msg));
120  if(ret<0)
121  {
122  strcpy(err,"Cannot write to port");
123  return -3;
124  }
125 
126  return 0;
127 }
128 
130 {
131  unsigned char msg = 0x00;
132  unsigned char data;
133  int nbytes_to_read=0;
134 
135  //Clean input buffer
136  CleanBuffer();
137 
138  ret = write(port,&msg,sizeof(msg));
139  if(ret<0)
140  {
141  strcpy(err,"Cannot write to port");
142  return -3;
143  }
144 
145  double ts=ros::Time::now().toSec();
146  double tl=0;
147  while(nbytes_to_read!=1)
148  {
149  ret=ioctl(port,FIONREAD,&nbytes_to_read);
150  usleep(1000);
151  tl=ros::Time::now().toSec()-ts;
152 
153  if(tl>1)
154  {
155  strcpy(err,"DIOC is not responding (read timeout)");
156  return -1;
157  }
158  }
159 
160  ret=read(port,&data,1);
161  if(ret<0)
162  {
163  strcpy(err,"Cannot read responce");
164  return -3;
165  }
166 
167  if(data == 0x80)
168  return 1;
169  else
170  return 0;
171 }
172 
174 {
175  unsigned char msg;
176  int nbytes_to_read=0;
177  unsigned char data;
178  unsigned char status;
179 
180  switch(ID)
181  {
182  case CROSS_A:
183  case CROSS_B:
184  msg=ID;
185  break;
186  default:
187  strcpy(err,"This IO does not respond its current state");
188  return -1;
189  }
190 
191  //Clean input buffer
192  CleanBuffer();
193 
194  ret = write(port,&msg,sizeof(msg));
195  if(ret<0)
196  {
197  strcpy(err,"Cannot write to port");
198  return -3;
199  }
200 
201  double ts=ros::Time::now().toSec();
202  double tl=0;
203  while(nbytes_to_read!=1)
204  {
205  ret=ioctl(port,FIONREAD,&nbytes_to_read);
206  usleep(1000);
207  tl=ros::Time::now().toSec()-ts;
208 
209  if(tl>1)
210  {
211  strcpy(err,"DIOC is not responding (read timeout)");
212  return -1;
213  }
214  }
215 
216  ret=read(port,&data,1);
217  if(ret<0)
218  {
219  strcpy(err,"Cannot read responce");
220  return -3;
221  }
222 
223  status = (data & 0x80) >> 7;
224 
225  return status;
226 }
227 
228 void class_dioc::perr(int ret)
229 {
230  if(ret==-1)
231  {
232  printf("Error!! ");
233  printf("%s (raising SIGINT)\n",err);
234  raise(SIGINT);
235  }else if(ret==-2)
236  {
237  printf("Warning!! ");
238  printf("%s\n",err);
239  }else if(ret==-3)
240  {
241  printf("Error!! ");
242  printf("%s, ",err);fflush(stdout);
243  perror(NULL);
244  raise(SIGINT);
245  }
246 }
char * device
Communication device to use.
Definition: class_dioc.h:141
int ret
Auxiliary return code.
Definition: class_dioc.h:138
class_dioc(const char *pdevice)
Constructor.
Definition: class_dioc.cpp:33
int SetStatus(id_enum ID, status_enum ENUM)
Set the status of a input/output.
Definition: class_dioc.cpp:110
bool active
This variable indicates that the communication is active.
Definition: class_dioc.h:132
int GetStatus(id_enum ID)
Get the status of a input/output.
Definition: class_dioc.cpp:173
int port
Communication port to use.
Definition: class_dioc.h:135
int CommStatus(void)
Get the comm status.
Definition: class_dioc.cpp:129
Includes, global vars, function prototypes, etc.
~class_dioc()
De-constructor.
Definition: class_dioc.cpp:69
void CleanBuffer(void)
Cleans the buffer.
Definition: class_dioc.cpp:75
char err[1024]
Definition: class_dioc.h:143
void perr(int ret)
Print error function.
Definition: class_dioc.cpp:228


atlasmv_base
Author(s): David Gameiro, Jorge Almeida
autogenerated on Mon Mar 2 2015 01:31:28