ViSP  2.8.0
servoCycab.cpp
1 /****************************************************************************
2  *
3  * $Id: servoCycab.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Send a command to the car-like Cycab mobile robot.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
50 #include <visp/vpConfig.h>
51 #include <iostream>
52 #include <cmath> // std::fabs
53 #include <limits> // numeric_limits
54 
55 #include <signal.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <math.h>
60 using namespace std;
61 
62 #ifdef VISP_HAVE_CYCAB
63 #include <visp/vpRobotCycab.h>
64 #include <visp/vpMath.h>
65 #include <visp/vpTime.h>
66 
67 
68 vpRobotCycab *cycab = NULL;
69 bool bFinish = false;
70 
71 bool end = false;
72 
73 // The first CTRL-C stop properly the car by decreasing the velocity
74 // and the steering angle, the second CTRL-C ends the execution
75 void sighdl(int n)
76 {
77  printf("Received signal %d\n",n);
78  bFinish=true;
79  end = true;
80 }
81 
82 #define MAXV 1.5 // velocity in m/S
83 #define MAXPHI 20.0 // front steering angle in deg
84 #define MAX_ACC_V 6 // m/s^2
85 #define MAX_VEL_PHI 4 // rad/s
86 
87 int main()
88 {
89  double v, phi; // Command to send
90  double vm, phim; // Measures
91  int kv,kphi;
92 
93  cycab = new vpRobotCycab();
94 
95  kv = kphi = 1;
96  v = 0.0;
97  phi = 0.0;
98  signal(SIGINT,sighdl);
99  signal(SIGTERM,sighdl);
100  signal(SIGPIPE,sighdl);
101 
102  double t0 = vpTime::measureTimeMs();
103  double t1 = vpTime::measureTimeMs();
104  double tprev;
105  bool ctrc = false;
106  double timestamp;
107 
108 
109  while (!end) {
110  tprev = t1;
111  t1 = vpTime::measureTimeMs();
112  // Measures the velocity and the front steering angle from odometry
113  cycab->getOdometry(vm, phim, timestamp); // measured values from odometry
114 
115  printf("State: t=%.1f s v=%f m/s and phi=%f deg\n\t",
116  (timestamp-t0)/1000, vm, vpMath::deg(phim));
117 
118  // Compute the command to apply to the car
119  if (1) {
120  v+=kv*0.002;if (fabs(v)>=MAXV) kv = -1 * kv;
121  phi+=kphi*0.002;if (fabs(phi)>= vpMath::rad(MAXPHI)) kphi = -1 * kphi;
122  }
123  else {
124  v=0.1;phi=0;
125  }
126 
127  // Check is CTRL-C is requested
128  if (bFinish) {
129  // we stop the Cycab by decreasing the velocity and the steering
130  // angle to zero
131  std::cout << "Cycab stop requested" << std::endl;
132  // Velocity decrease to zero
133  double sign_v = 0;
134  //if (vm != 0.)
135  if (std::fabs(vm) > std::numeric_limits<double>::epsilon())
136  sign_v = fabs(vm)/vm;
137  v = vm - MAX_ACC_V*(t1-tprev)/1000*sign_v;
138  if (fabs(v) < 0.1) v = 0;
139 
140  // Steering decrease to zero
141  double sign_phi = 0;
142  //if (phim != 0.)
143  if (std::fabs(phim) > std::numeric_limits<double>::epsilon())
144  sign_phi = fabs(phim)/phim;
145  phi = phim - MAX_VEL_PHI*(t1-tprev)/1000*sign_phi;
146  if (fabs(phi) < vpMath::rad(5)) phi = 0;
147 
148  // printf("stop requested: vm %f v %f phim %f phi %f sign_phi %f\n",
149  // vm, v, phim, phi, sign_phi);
150  //v = 0;
151  //phi = 0;
152  }
153 
154  // Send the command
155  printf("Send : v %f m/s and phi %f deg\n", v, vpMath::deg(phi));
156  cycab->setCommand(v, phi);
157 
158  vpTime::wait(10);
159 
160  if (end && (!ctrc)) { end = false; ctrc=true;}
161  }
162  std::cout << "The end" << std::endl;
163  return 0;
164 }
165 
166 #else // VISP_HAVE_CYCAB
167 int main()
168 {
169  cout << "Sorry no acces to the cycab" << endl;
170 }
171 #endif //VISP_HAVE_CYCAB
172 
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
Interface for the car-like Cycab mobile robot.
static double rad(double deg)
Definition: vpMath.h:100
static double deg(double rad)
Definition: vpMath.h:93