haptic_rendering_funx.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 hduVector3Dd CalculateWorkspaceDemoForce(void *pUserData)
35 {
36  shared_vars_t*RobotVars=(shared_vars_t*)pUserData;
37  //vectors
38  hduVector3Dd forceDirection;
39  //force vector initialization
40  hduVector3Dd sent_force(0.0,0.0,0.0);
41  //joystick position;
42  static hduVector3Dd phantom_position;
43 
44 
45  // ***** Calculations *****
46  if( (RobotVars->parameters.kinematic_model>=1 && RobotVars->parameters.kinematic_model<=3) && RobotVars->parameters.manual_speed_control)
47  {
48  // *** ARMS ***
49  //inside sphere properties
50  double min_sphereRadius = ((double)sqrt(13327.) / (double)RobotVars->parameters.pos_coord_scale); // mm
51  RobotVars->haptics_data.arm_main_sphere_min_Radius = min_sphereRadius;
52  //outside sphere properties
53  double max_sphereRadius = (RobotVars->humanoid_f->GetRobotDimension(ARM_LENGTH)+RobotVars->humanoid_f->GetRobotDimension(FOREARM_LENGTH))
54  /
55  ((double)RobotVars->parameters.pos_coord_scale); // mm
56  RobotVars->haptics_data.arm_main_sphere_max_Radius = max_sphereRadius;
57  //back sphere properties
58  double back_sphereRadius = RobotVars->humanoid_f->GetRobotDimension(FOREARM_LENGTH) / ((double)RobotVars->parameters.pos_coord_scale); // mm
59  RobotVars->haptics_data.arm_back_sphereRadius = back_sphereRadius;
60 
61 
62  // center of the main sphere
63  hduVector3Dd main_spheresPosition = RobotVars->haptics_data.arm_main_spheresPosition;
64  // center of the back sphere
65  hduVector3Dd back_spheresPosition = RobotVars->haptics_data.arm_back_spherePosition;
66 
67  //phantom position
68  phantom_position = RobotVars->phantom_data.m_devicePosition;
69 
70  //force direction is constant in Y axle / joystick X axle
71  if(RobotVars->parameters.kinematic_model==1)
72  {
73  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(hduVector3Dd(0.,-1.,0.));
74  // YY PLANE FORCE
75  sent_force = sent_force + (CalculatePolyForceMagnitude(-(phantom_position[1] - main_spheresPosition[1])/ (double)RobotVars->parameters.pos_coord_scale) * forceDirection);
76  }
77  else
78  {
79  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(hduVector3Dd(0.,1.,0.));
80  // YY PLANE FORCE
81  sent_force = sent_force + (CalculatePolyForceMagnitude((phantom_position[1] - main_spheresPosition[1])/ (double)RobotVars->parameters.pos_coord_scale) * forceDirection);
82  }
83 
84  // MAIN SPHERES:
85  double radius = (phantom_position - main_spheresPosition).magnitude();
86  // SMALL CENTER SPHERE
87  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(((phantom_position - main_spheresPosition)/radius));
88  sent_force = sent_force + ((CalculatePolyForceMagnitude(radius - min_sphereRadius)) * forceDirection);
89  // BIG CENTER SPHERE
90  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates((-1. * (phantom_position - main_spheresPosition)/radius));
91  sent_force = sent_force + ((CalculatePolyForceMagnitude(max_sphereRadius - radius)) * forceDirection);
92  // BACK SPHERE
93  radius = (phantom_position - back_spheresPosition).magnitude();
94  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(((phantom_position - back_spheresPosition)/radius));
95  sent_force = sent_force + ((CalculatePolyForceMagnitude(radius - back_sphereRadius)) * forceDirection);
96  }
97  else if((RobotVars->parameters.kinematic_model>=4 && RobotVars->parameters.kinematic_model<=5) && RobotVars->parameters.manual_speed_control)
98  {
99  //LEGS
100  //outside sphere properties
101  double max_sphereRadius = ((RobotVars->humanoid_f->GetRobotDimension(THIGH_LENGTH)
102  +
103  RobotVars->humanoid_f->GetRobotDimension(LEG_LENGTH)) / (double)RobotVars->parameters.pos_coord_scale); // mm
104  RobotVars->haptics_data.leg_main_sphere_max_Radius = max_sphereRadius;
105  //smaller sphere properties
106  double min_sphereRadius = ((double)119.233 / (double)RobotVars->parameters.pos_coord_scale); // mm
107  RobotVars->haptics_data.leg_main_sphere_min_Radius = min_sphereRadius;
108  //front small sphere properties
109  double min_front_sphereRadius = (RobotVars->humanoid_f->GetRobotDimension(LEG_LENGTH) / (double)RobotVars->parameters.pos_coord_scale); // mm
110  RobotVars->haptics_data.leg_back_sphereRadius = min_front_sphereRadius;
111  //back outer sphere properties
112  double max_back_sphereRadius = (RobotVars->humanoid_f->GetRobotDimension(LEG_LENGTH) / (double)RobotVars->parameters.pos_coord_scale); // mm
113  if(max_back_sphereRadius){}
114 
115  phantom_position = RobotVars->phantom_data.m_devicePosition;
116 
117  // centers of the spheres
118  hduVector3Dd main_spheresPosition = RobotVars->haptics_data.leg_main_spheresPosition;
119  hduVector3Dd back_spheresPosition = RobotVars->haptics_data.leg_back_spherePosition;
120  hduVector3Dd front_spheresPosition = RobotVars->haptics_data.leg_front_spherePosition;
121 
122  // OUTER SPHERE:
123  double radius = (phantom_position - main_spheresPosition).magnitude();
124  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates((-1. * (phantom_position - main_spheresPosition)/radius));
125  sent_force = sent_force + ((CalculatePolyForceMagnitude(max_sphereRadius - radius)) * forceDirection);
126  // INNER SPHERE
127  forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(((phantom_position - main_spheresPosition)/radius));
128  sent_force = sent_force + ((CalculatePolyForceMagnitude(radius - min_sphereRadius)) * forceDirection);
129  //PLANES
130  //vector u
131  hduVector3Dd u = hduVector3Dd(1.0,0.0,0.0);
132  //vector v30
133  hduVector3Dd v30 = hduVector3Dd(0.0,-sin(DegToRad(30.0)),cos(DegToRad(30.0)));
134  //vector v45
135  hduVector3Dd v45 = hduVector3Dd(0.0,sin(DegToRad(45.0)),cos(DegToRad(45.0)));
136 
137 // if(RobotVars->parameters.kinematic_model==4)
138 // {
139 // //LEFT LEG
140 //
141 //
142 // }
143 // else
144 // {
145 // //RIGHT LEG
146 // // vector normal to plane on the left
147 // hduVector3Dd n_left = crossProduct(u,v30);
148 // // equation of a plane s = a*x + b*y + c*z + d
149 // double d = dotProduct( -main_spheresPosition + hduVector3Dd(0.0,-sin(DegToRad(30.0)),cos(DegToRad(30.0))), n_left );
150 // n_left = -n_left;
151 // //distance to the plane s
152 // double s = (n_left[0] * phantom_position[0] + n_left[1] * phantom_position[1] + n_left[2] * phantom_position[2] + d) / sqrt(pow(n_left[0],2.) + pow(n_left[1],2.) + pow(n_left[2],2.));
153 // s = s / (double)RobotVars->parameters.pos_coord_scale;
154 // std::cout<<"DISTANCE TO PLANE"<<s<<std::endl;
155 // n_left.normalize();
156 // forceDirection = n_left;
157 // sent_force = sent_force + ((CalculatePolyForceMagnitude(s)) * forceDirection);
158 
159 // // vector normal to plane on the right
160 // hduVector3Dd n_right = crossProduct(u,v45);
161 // // equation of a plane s = a*x + b*y + c*z + d
162 // d = dotProduct( -hduVector3Dd(0.0,sin(DegToRad(45.0)),cos(DegToRad(45.0))), n_right );
163 // //distance to the plane s
164 // s = (n_right[0] * phantom_position[0] + n_right[1] * phantom_position[1] + n_right[2] * phantom_position[2] + d) / sqrt(pow(n_right[0],2.) + pow(n_right[1],2.) + pow(n_right[2],2.));
165 // s = s / (double)RobotVars->parameters.pos_coord_scale;
166 // n_right.normalize();
167 // forceDirection = n_right;
168 // sent_force = sent_force + ((CalculatePolyForceMagnitude(s)) * forceDirection);
169 // }
170 // // FRONT SPHERE
171 // radius = (phantom_position - front_spheresPosition).magnitude();
172 // forceDirection = TransformWorldCoordinatesToPHaNToMCoordinates(((phantom_position - front_spheresPosition)/radius));
173 // sent_force = sent_force + ((CalculatePolyForceMagnitude(radius - min_front_sphereRadius)) * forceDirection);
174  }
175  else
176  {
177  //OTHERS
178  }
179 
180  return (sent_force);
181 }
182 
183 hduVector3Dd CalculatePlaneDrawingDemoForce(void *pUserData)
184 {
185  shared_vars_t*RobotVars=(shared_vars_t*)pUserData;
186 
187  hduVector3Dd joystick_position = RobotVars->phantom_data.m_devicePosition;
188 
189  HDdouble s = RobotVars->haptics_data.demo_2_Plane.perpDistance(joystick_position);
190 
191  RobotVars->haptics_data.demo2_distance_to_plane = fabs((double)s / (double)RobotVars->parameters.pos_coord_scale);
192  hduVector3Dd n = RobotVars->haptics_data.demo_2_Plane.normal();
193  n.normalize();
194 
195  // force calculation
197 }
198 
199 void Demo2_BuildPlane(void *pUserData)
200 {
201  shared_vars_t*RobotVars=(shared_vars_t*)pUserData;
202  // use plane class from openhaptics
203  RobotVars->haptics_data.demo_2_Plane = hduPlaned(RobotVars->haptics_data.demo_2_point_1,
204  RobotVars->haptics_data.demo_2_point_2,
205  RobotVars->haptics_data.demo_2_point_3);
206 }
207 
208 hduVector3Dd Leg_COP_Monitoring(void *pUserData)
209 {
210  shared_vars_t*RobotVars=(shared_vars_t*)pUserData;
211  hduVector3Dd Fx,Fy;
212  double x,y;
213  static double x_border_p, x_border_m, y_border_p, y_border_m;
214 
215  if(RobotVars->parameters.kinematic_model==4)
216  {
217  x = RobotVars->robot_kin_data.COG_detached_leg_left[0];
218  y = RobotVars->robot_kin_data.COG_detached_leg_left[1];
219  }
220  else if(RobotVars->parameters.kinematic_model==5)
221  {
222  x = RobotVars->robot_kin_data.COG_detached_leg_right[0];
223  y = RobotVars->robot_kin_data.COG_detached_leg_right[1];
224  }
225  else
226  return hduVector3Dd(0.0,0.0,0.0);
227 
228  std::cout<<"X: "<<x<<std::endl;
229  std::cout<<"Y: "<<y<<std::endl;
230 
231  if( fabs(y) >= 10.0)
232  {
233  Fy = hduVector3Dd(0.0,-CalculateExponentialForceMagnitude(0.0),0.0);
234  }
235  if( fabs(x) >= 35.0)
236  {
237  Fx = hduVector3Dd(CalculateExponentialForceMagnitude(1),0.0,0.0);
238  }
239 
240 // x_border_p = RobotVars->haptics_data.leg_main_spheresPosition[0] + (80.0)/2.0;
241 // x_border_m = RobotVars->haptics_data.leg_main_spheresPosition[0] - (80.0)/2.0;
242 // y_border_p = RobotVars->haptics_data.leg_main_spheresPosition[1] + (RobotVars->humanoid_f->GetRobotDimension(FOOT_WIDTH))/2.0;
243 // y_border_m = RobotVars->haptics_data.leg_main_spheresPosition[1] - (RobotVars->humanoid_f->GetRobotDimension(FOOT_WIDTH))/2.0;
244 //
245 // double s;
246 // //check x
247 // s = x_border_p - x;
248 // Fx = hduVector3Dd(-1*get_sign(x)*CalculateExponentialForceMagnitude(s),0.0,0.0);
249 // s = x-x_border_m;
250 // Fx += hduVector3Dd(get_sign(x)*CalculateExponentialForceMagnitude(s),0.0,0.0);
251 //
252 // //check y
253 // s = y_border_p - y;
254 // Fy = hduVector3Dd(0.0,-1*get_sign(y)*CalculateExponentialForceMagnitude(s),0.0);
255 // s = y-y_border_m;
256 // Fy += hduVector3Dd(0.0,get_sign(y)*CalculateExponentialForceMagnitude(s),0.0);
257 //
258 // std::cout<<Fx+Fy<<std::endl;
259 
260  return Fx+Fy;
261 }
262 
263 
264 
265 
#define ARM_LENGTH
haptic_rendering_funx.h file for this module. Contains includes, prototypes and global vars...
hduPlaned demo_2_Plane
Definition: types.h:266
hduVector3Dd TransformWorldCoordinatesToPHaNToMCoordinates(hduVector3Dd source)
Function to rotate world oriented vector to PHANToM referencial.
double arm_main_sphere_max_Radius
Definition: types.h:251
int kinematic_model
Definition: types.h:229
double arm_main_sphere_min_Radius
Definition: types.h:252
#define FOREARM_LENGTH
double GetRobotDimension(int dimension)
Outputs selected robot dimension length.
Type CalculatePolyForceMagnitude(Type x)
Function to determine force values using the polynomial force behaviour law.
double leg_main_sphere_min_Radius
Definition: types.h:258
HapticsData haptics_data
Definition: types.h:289
hduVector3Dd arm_back_spherePosition
Definition: types.h:250
humanoid * humanoid_f
Definition: types.h:290
#define THIGH_LENGTH
double demo2_distance_to_plane
Definition: types.h:267
hduVector3Dd demo_2_point_1
Definition: types.h:263
Eigen::Vector3d COG_detached_leg_right
Definition: types.h:106
Shared struture that holds robot/device information.
Definition: types.h:279
hduVector3Dd Leg_COP_Monitoring(void *pUserData)
Function that tracks the detached leg COP and creates a force vector.
Eigen::Vector3d COG_detached_leg_left
Definition: types.h:105
double pos_coord_scale
Definition: types.h:235
hduVector3Dd demo_2_point_3
Definition: types.h:265
#define LEG_LENGTH
hduVector3Dd leg_back_spherePosition
Definition: types.h:255
ParameterSet parameters
Definition: types.h:286
double leg_back_sphereRadius
Definition: types.h:259
hduVector3Dd CalculateWorkspaceDemoForce(void *pUserData)
Function to define the workspace haptic demo force.
bool manual_speed_control
Definition: types.h:230
double arm_back_sphereRadius
Definition: types.h:253
hduVector3Dd arm_main_spheresPosition
Definition: types.h:249
hduVector3Dd m_devicePosition
Definition: types.h:202
void Demo2_BuildPlane(void *pUserData)
Function to define the plane for plane drawing demo.
Type CalculatePlaneForceMagnitude(Type x)
Function to determine force values of the plane in the plane drawing demo.
hduVector3Dd leg_main_spheresPosition
Definition: types.h:254
double leg_main_sphere_max_Radius
Definition: types.h:257
RobotKinData robot_kin_data
Definition: types.h:282
hduVector3Dd CalculatePlaneDrawingDemoForce(void *pUserData)
Function to define the plane drawing haptic demo force.
Type DegToRad(Type deg)
Templated degree to radian conversion function.
Definition: miscellaneous.h:79
DeviceData phantom_data
Definition: types.h:285
Type CalculateExponentialForceMagnitude(Type x)
Function to determine force values using the exponential force behaviour law.
hduVector3Dd demo_2_point_2
Definition: types.h:264
hduVector3Dd leg_front_spherePosition
Definition: types.h:256


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