miscellaneous.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 ***************************************************************************************************/
33 
34 /*~~~~~~~~~~~~~~~~~
35 || help ||
36 ~~~~~~~~~~~~~~~~~~*/
37 void print_help(void)
38 {
39  const char help[]={"***************************************************************************+\n\
40  ------------------------------ |\n\
41  | PHUA HAPTIC INTERFACE HELP | |\n\
42  ------------------------------ |\n\
43  |\n\
44  This program uses RS232 comunication over an USB adapter to send |\n\
45  commands to the humanoid robot. |\n\
46  If you're having trouble connecting, use the path to your USB port |\n\
47  as an argument when you call the application. |\n\
48  |\n\
49  example: >> ./phua_haptic_interface /dev/ttyUSB* |\n\
50  [where * is the number of your port] |\n\
51  |\n\
52 ---------------------------------------------------------------------------+\n\
53  |\n\
54  The PHANToM OMNI joystick functions require a FireWire port active |\n\
55  and superuser permissions to access it. |\n\
56  If you are having trouble with this, open a terminal and run: |\n\
57  |\n\
58  >> sudo modprobe raw1394 |\n\
59  |\n\
60  This will probe the FireWire port the joystick connection. If it is |\n\
61  sucessful, module raw1394 should appear in your ´/dev/´ system folder. |\n\
62  |\n\
63  For the program to have permission to communicate with the device, call |\n\
64  this application with superuser rights using ´sudo´ or run within |\n\
65  a terminal: |\n\
66  |\n\
67  >> sudo chmod 777 /dev/raw1394 |\n\
68  |\n\
69  This should allow normal execution of the program. |\n\
70  |\n\
71 ---------------------------------------------------------------------------+\n\
72  |\n\
73  To run aplication in debug mode, use argument ´--debug´. |\n\
74  In this mode, application will execute even if connections are |\n\
75  not established. Keep in mind that the application may not |\n\
76  properly control the robot or retrieve data from the PHANToM OMNI |\n\
77  joystick in this mode, and a restart is required in order to |\n\
78  use the program's full funcionalities. |\n\
79  |\n\
80  example: >> ./phua_haptic_interface --debug |\n\
81  |\n\
82 ---------------------------------------------------------------------------+\n\
83  |\n\
84  Pedro Cruz 2012 |\n\
85  Happy Haptics! :-) |\n\
86 ***************************************************************************+"
87  };
88  printf("%s\n",help);
89 }
90 
92 {
93  char servo_comm_on[15]=" ";
94  if(RobotVars->servo->IsActive())
95  strcat(servo_comm_on,"ONLINE");
96  else
97  strcat(servo_comm_on,"OFFLINE");
98 
99  char phant_on[15]=" ";
100  if(RobotVars->phantom_data.phantom_on)
101  strcat(phant_on,"ONLINE");
102  else
103  strcat(phant_on,"OFFLINE");
104 
105  printf("***************************************************************************\n\
106  ------------------------------------- \n\
107  | PHUA HAPTIC INTERFACE APPLICATION | \n\
108  ------------------------------------- \n\
109  \n\
110  Pedro Cruz 2012 \n\
111 ***************************************************************************\n\
112  \n\
113  REQUIRED COMUNICATION STATUS \n\
114  ---------------------------------- \n\
115  > Servomotor COMM: |%s | \n\
116  > PHANToM OMNI COMM: |%s | \n\
117  ---------------------------------- \n",
118  servo_comm_on,phant_on);
119 }
120 
121 void CalculateAverageCartesianSpeed(double diffX, double diffY, double diffZ, long double time_interval, double *return_vector)
122 {
123  static const uint array_size=20;
124 
125  static std::vector<double> accumulationXdiff;
126  static std::vector<double> accumulationYdiff;
127  static std::vector<double> accumulationZdiff;
128  static std::vector<long double> accumulation_time;
129 
130  accumulationXdiff.push_back(diffX);
131  accumulationYdiff.push_back(diffY);
132  accumulationZdiff.push_back(diffZ);
133  accumulation_time.push_back(time_interval);
134 
135  if(accumulationXdiff.size()>array_size)
136  accumulationXdiff.erase(accumulationXdiff.begin());
137 
138  if(accumulationYdiff.size()>array_size)
139  accumulationYdiff.erase(accumulationYdiff.begin());
140 
141  if(accumulationZdiff.size()>array_size)
142  accumulationZdiff.erase(accumulationZdiff.begin());
143 
144  if(accumulation_time.size()>array_size)
145  accumulation_time.erase(accumulation_time.begin());
146 
147  double x_mean=pc_mean<double>(accumulationXdiff);
148  double y_mean=pc_mean<double>(accumulationYdiff);
149  double z_mean=pc_mean<double>(accumulationZdiff);
150  long double time_mean=pc_mean<long double>(accumulation_time);
151 
152  return_vector[0]=round(x_mean/time_mean);
153  return_vector[1]=round(y_mean/time_mean);
154  return_vector[2]=round(z_mean/time_mean);
155 }
156 
157 Eigen::Matrix3d rotx(double angle_in_degrees)
158 {
159  static Eigen::Matrix3d rotation_matrix;
160  static double angle_in_radians;
161 
162  angle_in_radians = DegToRad(angle_in_degrees);
163 
164  for(int i=0; i<3; i++)
165  {
166  for(int j=0; j<3; j++)
167  {
168  if((i==0 && j==0))
169  rotation_matrix(i,j) = 1;
170  else if((i==1 && j==1) || (i==2 && j==2))
171  rotation_matrix(i,j) = cos(angle_in_radians);
172  else if((i==1 && j==2))
173  rotation_matrix(i,j) = -1 * sin(angle_in_radians);
174  else if((i==2 && j==1))
175  rotation_matrix(i,j) = sin(angle_in_radians);
176  else
177  rotation_matrix(i,j) = 0;
178  }
179  }
180  return rotation_matrix;
181 }
182 
183 Eigen::Matrix3d roty(double angle_in_degrees)
184 {
185  static Eigen::Matrix3d rotation_matrix;
186  static double angle_in_radians;
187 
188  angle_in_radians = DegToRad(angle_in_degrees);
189 
190  for(int i=0; i<3; i++)
191  {
192  for(int j=0; j<3; j++)
193  {
194  if(i==1 && j==1)
195  rotation_matrix(i,j) = 1;
196  else if((i==0 && j==0) || (i==2 && j==2))
197  rotation_matrix(i,j) = cos(angle_in_radians);
198  else if((i==0 && j==2))
199  rotation_matrix(i,j) = sin(angle_in_radians);
200  else if((i==2 && j==0))
201  rotation_matrix(i,j) = -1. * sin(angle_in_radians);
202  else
203  rotation_matrix(i,j) = 0;
204  }
205  }
206  return rotation_matrix;
207 }
208 
209 Eigen::Matrix3d rotz(double angle_in_degrees)
210 {
211  static Eigen::Matrix3d rotation_matrix;
212  static double angle_in_radians;
213 
214  angle_in_radians = DegToRad(angle_in_degrees);
215 
216  for(int i=0; i<3; i++)
217  {
218  for(int j=0; j<3; j++)
219  {
220  if(i==2 && j==2)
221  rotation_matrix(i,j) = 1;
222  else if((i==0 && j==0) || (i==1 && j==1))
223  rotation_matrix(i,j) = cos(angle_in_radians);
224  else if((i==1 && j==0))
225  rotation_matrix(i,j) = sin(angle_in_radians);
226  else if((i==0 && j==1))
227  rotation_matrix(i,j) = -1. * sin(angle_in_radians);
228  else
229  rotation_matrix(i,j) = 0;
230  }
231  }
232  return rotation_matrix;
233 }
234 
235 double pc_mean(double *v, int len)
236 {
237  double sum = 0;
238  int i;
239  for (i = 0; i < len; i++)
240  sum += v[i];
241  return sum / len;
242 }
243 
244 int isNumeric (const char * s)
245 {
246  if (s == NULL || *s == '\0' || isspace(*s))
247  return 0;
248  char * p;
249  int r=strtod (s, &p);
250  if(r){}
251  return *p == '\0';
252 }
253 
254 double get_sign(double x)
255 {
256  return ((x >= 0.) ? 1. : -1.);
257 }
258 
260 {
261  int s;
262  int ret=scanf("%d",&s);
263  if(ret){}
264  return s;
265 }
266 
268 {
269  char s;
270  int ret=scanf("%s",&s);
271  if(ret){}
272  return s;
273 }
274 
275 double GetDblFromKeyboard(void)
276 {
277  float s;
278  int ret=scanf("%f",&s);
279  if(ret){}
280  return s;
281 }
282 
283 void ScreenClear(void)
284 {
285  printf("\033[2J");
286  printf("\033[0;0f");
287 }
288 
289 void textcolor(int attr, int fg, int bg)
290 {
291  printf("%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
292 }
293 
294 void PrintRedLine(void)
295 {
296  int n;
298  for(n=1;n<41;n++)
299  printf("=");
300  printf("\n");
301 }
302 
303 void ResetTextColors(void)
304 {
306 }
307 
308 void HighLightText(void)
309 {
311 }
void ScreenClear(void)
Sends equivalent shell command "clean" to stdout.
#define WHITE
void CalculateAverageCartesianSpeed(double diffX, double diffY, double diffZ, long double time_interval, double *return_vector)
Function acumulate position differences and calculate average speed.
char GetStrFromKeyboard(void)
Gets a character from keyboard input.
#define BLACK
Shared struture that holds robot/device information.
Definition: types.h:279
#define RESET
double pc_mean(double *v, int len)
Function to calculate mean value of an array [Taken from http://rosettacode.org/].
Eigen::Matrix3d rotz(double angle_in_degrees)
Function to calculate rotation matrix over the Z axis.
hitec_5980SG * servo
Definition: types.h:281
bool phantom_on
Definition: types.h:207
void PrintRedLine(void)
Printf of an horizontal separator red line.
void textcolor(int attr, int fg, int bg)
FUNCTIONS CREATED BY VITOR SANTOS(vitor@ua.pt) | ALL CREDITS GO TO HIM... :-)
int isNumeric(const char *s)
Function to check if string is numeric.
void ResetTextColors(void)
Reset color text.
#define RED
Eigen::Matrix3d roty(double angle_in_degrees)
Function to calculate rotation matrix over the Y axis.
double GetDblFromKeyboard(void)
Gets a double number from keyboard input.
int GetNbrFromKeyboard(void)
Gets a number from keyboard input.
void HighLightText(void)
Make somesort of highlight text.
void print_help(void)
Prints to stdout the program help tips.
#define BRIGHT
double get_sign(double x)
Function to determine sign of a given number.
Eigen::Matrix3d rotx(double angle_in_degrees)
Function to calculate rotation matrix over the X axis.
miscellaneous.h file for this module. Contains prototypes, includes and defines.
Type DegToRad(Type deg)
Templated degree to radian conversion function.
Definition: miscellaneous.h:79
DeviceData phantom_data
Definition: types.h:285
void print_greeting(shared_vars_t *RobotVars)
Prints to stdout the program greeting.


phua_haptic
Author(s): Pedro Cruz
autogenerated on Mon Mar 2 2015 01:32:36