calibration.cpp
Go to the documentation of this file.
1 
9 // MARTY
11 
12 Calibration::Calibration(ros::NodeHandle& nh) : nh_(nh) {
13  this->loadParams();
14  this->init();
15  this->rosSetup();
16  ROS_DEBUG("Marty Calibration Ready!");
17 }
18 
20  ros::param::del("/marty");
21 }
22 
24  nh_.param("calibrated", calibrated_, false);
25  if (!calibrated_) {
26  WARN("Marty has not been calibrated before!" << std::endl);
27  }
28 }
29 
31  enabled_ = true;
32  saving_ = false;
33  exiting_ = false;
34  marty_msgs::ServoMsg joint;
35  for (int id = 0; id < NUMJOINTS; ++id) {
36  joint.servo_id = id;
37  joints_.servo_msg.push_back(joint);
38  cal_vals_.servo_msg.push_back(joint);
39  }
40  for (int id = 0; id < NUMJOINTS; ++id) {
41  int val;
42  nh_.param(NAMES[id] + "/zero", val, 0);
43  joints_.servo_msg[id].servo_cmd = val;
44  cal_vals_.servo_msg[id].servo_cmd = val;
45  }
46 }
47 
49  joint_pub_ = nh_.advertise<marty_msgs::ServoMsgArray>("servo_array", 10);
50  enable_pub_ = nh_.advertise<std_msgs::Bool>("enable_motors", 10);
51 }
52 
54  CLEAR();
55  INFO(" q-a\tw-s\te-d\tr-f\tt-g\ty-h\tz-x\tc-v\tu-j\ti-k\to-l\tENTER to save\n");
56  INFO(" LHIP\tLTWIST\tLKNEE\tRHIP\tRTWIST\tRKNEE\tLARM\tRARM\tEYES\tAUX1\tAUX2\t");
57  if (enabled_) { WARN("n - Enabled\n") } else { ERR("n - Disabled\n"); }
58  WARN("Old:");
59  for (int id = 0; id < NUMJOINTS; ++id) {
60  INFO(int(cal_vals_.servo_msg[id].servo_cmd) << "\t")
61  }
62  WARN("m - Exit" << std::endl);
63  WARN("New:")
64  for (int id = 0; id < NUMJOINTS; ++id) {
65  INFO(int(joints_.servo_msg[id].servo_cmd) << "\t")
66  }
67 
68  // Publish if Robot is enabled
69  std_msgs::Bool enabled;
70  enabled.data = enabled_;
71  enable_pub_.publish(enabled);
72  // Publish Joint Commands
73  if (enabled_) {
74  joint_pub_.publish(joints_);
75  }
76 
77  if (!exiting_) {
78  if (!saving_) {
79  if ((c_ = getch())) {
80  ROS_INFO_STREAM("Char: " << c_);
81  ROS_INFO_STREAM("SAVING: " << saving_ << " EXITING: " << exiting_);
82  if (c_ == 'q') { joints_.servo_msg[LHIP].servo_cmd++; }
83  if (c_ == 'a') { joints_.servo_msg[LHIP].servo_cmd--; }
84  if (c_ == 'w') { joints_.servo_msg[LTWIST].servo_cmd++; }
85  if (c_ == 's') { joints_.servo_msg[LTWIST].servo_cmd--; }
86  if (c_ == 'e') { joints_.servo_msg[LKNEE].servo_cmd++; }
87  if (c_ == 'd') { joints_.servo_msg[LKNEE].servo_cmd--; }
88  if (c_ == 'r') { joints_.servo_msg[RHIP].servo_cmd++; }
89  if (c_ == 'f') { joints_.servo_msg[RHIP].servo_cmd--; }
90  if (c_ == 't') { joints_.servo_msg[RTWIST].servo_cmd++; }
91  if (c_ == 'g') { joints_.servo_msg[RTWIST].servo_cmd--; }
92  if (c_ == 'y') { joints_.servo_msg[RKNEE].servo_cmd++; }
93  if (c_ == 'h') { joints_.servo_msg[RKNEE].servo_cmd--; }
94  if (c_ == 'z') { joints_.servo_msg[LARM].servo_cmd++; }
95  if (c_ == 'x') { joints_.servo_msg[LARM].servo_cmd--; }
96  if (c_ == 'c') { joints_.servo_msg[RARM].servo_cmd++; }
97  if (c_ == 'v') { joints_.servo_msg[RARM].servo_cmd--; }
98  if (c_ == 'u') { joints_.servo_msg[EYES].servo_cmd++; }
99  if (c_ == 'j') { joints_.servo_msg[EYES].servo_cmd--; }
100  if (c_ == 'i') { joints_.servo_msg[AUX1].servo_cmd++; }
101  if (c_ == 'k') { joints_.servo_msg[AUX1].servo_cmd--; }
102  if (c_ == 'o') { joints_.servo_msg[AUX2].servo_cmd++; }
103  if (c_ == 'l') { joints_.servo_msg[AUX2].servo_cmd--; }
104  if (c_ == 'n') { enabled_ = !enabled_; }
105  if (c_ == '\n') { saving_ = true; }
106  if (c_ == 'm') { exiting_ = true; }
107  }
108  // Make sure the joint values do not go over the limit
109  for (int id = 0; id < NUMJOINTS; ++id) {
110  joints_.servo_msg[id].servo_cmd =
111  std::min(int(joints_.servo_msg[id].servo_cmd), 126);
112  joints_.servo_msg[id].servo_cmd =
113  std::max(int(joints_.servo_msg[id].servo_cmd), -126);
114  }
115  } else {
116  WARN("Are you sure you wish to save these values? (y/n) ");
117  if ((c_ = getch())) {
118  if (c_ == 'y') {
119  this->writeCalVals();
120  INFO("Saved!\n");
121  ros::shutdown();
122  } else { saving_ = false; }
123  }
124  }
125  } else {
126  WARN("Are you sure you want to exit? (y/n) ");
127  if ((c_ = getch())) {
128  if (c_ == 'y') {
129  INFO("Exiting!\n");
130  ros::shutdown();
131  } else { exiting_ = false; }
132  }
133  }
134 }
135 
137  std::string path = ros::package::getPath("ros_marty");
138  std::ofstream of (path + "/cfg/joint_calib.cfg");
139  of << "# ****************************************************************************\n"
140  << "# **WARNING: DO NOT CHANGE ANYTHING BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!**\n"
141  << "# ****************************************************************************\n";
142  of << "calibrated: true\n\n";
143  for (int id = 0; id < NUMJOINTS; ++id) {
144  std::string name = NAMES[id];
145  int off(0);
146  int val = int(joints_.servo_msg[id].servo_cmd);
147  if ((name == "LHIP") or (name == "RHIP")) {off = HIP_OFFSET;}
148  else if ((name == "LTWIST") or (name == "RTWIST")) {off = TWIST_OFFSET;}
149  else if ((name == "LKNEE") or (name == "RKNEE")) {off = KNEE_OFFSET;}
150  else if ((name == "LARM") or (name == "RARM")) {off = ARM_OFFSET;}
151  else if (name == "EYES") {off = EYES_OFFSET;}
152  else if ((name == "AUX1") or (name == "AUX2")) {off = AUX_OFFSET;}
153  else {WARN("No offset found for joint: " << name << std::endl);}
154  of << name << ": {min: " << std::max(val - off, -126) <<
155  ", zero: " << val << ", max: " << std::min(val + off, 126) <<
156  ", dir: " << JOINT_DIR[id] << ", mult: " << JOINT_MULT[id] << "}\n";
157  }
158  of.close();
159 }
160 
162  char buf = 0;
163  struct termios old = {0};
164  if (tcgetattr(0, &old) < 0)
165  perror("tcsetattr()");
166  old.c_lflag &= ~ICANON;
167  old.c_lflag &= ~ECHO;
168  old.c_cc[VMIN] = 1;
169  old.c_cc[VTIME] = 0;
170  if (tcsetattr(0, TCSANOW, &old) < 0)
171  perror("tcsetattr ICANON");
172  if (read(0, &buf, 1) < 0)
173  perror ("read()");
174  old.c_lflag |= ICANON;
175  old.c_lflag |= ECHO;
176  if (tcsetattr(0, TCSADRAIN, &old) < 0)
177  perror ("tcsetattr ~ICANON");
178  return (buf);
179 }
void loadParams()
Definition: calibration.cpp:23
ros::NodeHandle nh_
Definition: calibration.hpp:32
const std::vector< int > JOINT_DIR
Definition: definitions.hpp:58
#define CLEAR()
Definition: definitions.hpp:25
#define WARN(x)
Definition: definitions.hpp:22
#define ARM_OFFSET
ros::Publisher joint_pub_
Definition: calibration.hpp:63
void rosSetup()
Definition: calibration.cpp:48
marty_msgs::ServoMsgArray joints_
Definition: calibration.hpp:59
marty_msgs::ServoMsgArray cal_vals_
Definition: calibration.hpp:60
#define HIP_OFFSET
const std::vector< std::string > NAMES
Definition: definitions.hpp:44
#define EYES_OFFSET
ros::Publisher enable_pub_
Definition: calibration.hpp:64
#define AUX_OFFSET
void calibrate()
Definition: calibration.cpp:53
bool calibrated_
Definition: calibration.hpp:53
#define ERR(x)
Definition: definitions.hpp:21
const std::vector< int > JOINT_MULT
Definition: definitions.hpp:72
void writeCalVals()
#define INFO(x)
Definition: definitions.hpp:23
Calibration class for calibrating Marty&#39;s servos.
#define TWIST_OFFSET
#define KNEE_OFFSET
Calibration(ros::NodeHandle &nh)
Definition: calibration.cpp:12


ros_marty
Author(s): Alejandro Bordallo
autogenerated on Fri Jul 21 2017 14:51:45