Revision 75a7571a
ID | 75a7571a58c8e315beec4bf6a966437fdc0765b7 |
Finished skeleton of cliffsensor node. Compiles.
scout/cliffsensor/CMakeLists.txt | ||
---|---|---|
29 | 29 |
#rosbuild_add_executable(example examples/example.cpp) |
30 | 30 |
#target_link_libraries(example ${PROJECT_NAME}) |
31 | 31 |
|
32 |
rosbuild_add_executable(motors_node src/motors.cpp) |
|
32 |
rosbuild_add_executable(cliffsensor_node src/cliffsensor.cpp) |
scout/cliffsensor/src/cliffsensor.cpp | ||
---|---|---|
52 | 52 |
static bool is_cliff; /**< Boolean to represent whether or not there is a cliff. */ |
53 | 53 |
|
54 | 54 |
/** |
55 |
* @brief Sets motor speed
|
|
55 |
* @brief Changes cliff sensor status
|
|
56 | 56 |
* |
57 |
* Sets the motor speeds based on subscription to the set_motors topic.
|
|
57 |
* Changes cliff sensor status based on subscription to topic cliff_status_changed
|
|
58 | 58 |
* |
59 |
* @param msg The message from the set_motors topic, containing speeds and
|
|
60 |
* motor configuration settings.
|
|
59 |
* @param msg The message from the cliff_status_changed topic, containing
|
|
60 |
* status of all cliff sensors.
|
|
61 | 61 |
*/ |
62 |
void motors_set(const motors::set_motors::ConstPtr& msg)
|
|
62 |
void changed_cliff_status(const cliffsensor::cliff_status_changed::ConstPtr& msg)
|
|
63 | 63 |
{ |
64 |
/** @todo Edit to only set requested motors, not all */ |
|
65 |
int which = msg->which; |
|
66 |
if(which & MOTOR_FL_REV) |
|
67 |
{ |
|
68 |
motor_fl_speed = -1 * motors_rel_to_abs(msg->fl_speed, msg->units); |
|
69 |
} |
|
70 |
if(which & MOTOR_FR_REV) |
|
71 |
{ |
|
72 |
motor_fr_speed = -1 * motors_rel_to_abs(msg->fr_speed, msg->units); |
|
73 |
} |
|
74 |
if(which & MOTOR_BL_REV) |
|
75 |
{ |
|
76 |
motor_bl_speed = -1 * motors_rel_to_abs(msg->bl_speed, msg->units); |
|
77 |
} |
|
78 |
if(which & MOTOR_BR_REV) |
|
79 |
{ |
|
80 |
motor_br_speed = -1 * motors_rel_to_abs(msg->br_speed, msg->units); |
|
81 |
} |
|
82 |
if(which & MOTOR_FL) |
|
83 |
{ |
|
84 |
motor_fl_speed = motors_rel_to_abs(msg->fl_speed, msg->units); |
|
85 |
} |
|
86 |
if(which & MOTOR_FR) |
|
87 |
{ |
|
88 |
motor_fr_speed = motors_rel_to_abs(msg->fr_speed, msg->units); |
|
89 |
} |
|
90 |
if(which & MOTOR_BL) |
|
91 |
{ |
|
92 |
motor_bl_speed = motors_rel_to_abs(msg->bl_speed, msg->units); |
|
93 |
} |
|
94 |
if(which & MOTOR_BR) |
|
95 |
{ |
|
96 |
motor_br_speed = motors_rel_to_abs(msg->br_speed, msg->units); |
|
97 |
} |
|
98 |
|
|
99 |
/* Write speeds to hardware */ |
|
100 |
/** @todo Add code to write speeds to hardware */ |
|
64 |
return; |
|
101 | 65 |
} |
102 | 66 |
|
103 | 67 |
/** |
104 |
* @brief Outputs current motor speeds
|
|
68 |
* @brief Outputs status of cliffsensors
|
|
105 | 69 |
* |
106 |
* Serves the service query_motors by responding to service requests with the
|
|
107 |
* speeds of the motors.
|
|
70 |
* Serves the service query_cliff by responding to service requests with the
|
|
71 |
* status of the cliff sensors.
|
|
108 | 72 |
* @param req The request. The only field is the units requested. |
109 | 73 |
* @param res The response. The fields will be filled with values. |
110 | 74 |
*/ |
111 |
bool motors_query(motors::query_motors::Request &req,
|
|
112 |
motors::query_motors::Response &res)
|
|
75 |
bool cliff_query(cliffsensor::query_cliff::Request &req,
|
|
76 |
cliffsensor::query_cliff::Response &res)
|
|
113 | 77 |
{ |
114 |
int units = req.units;
|
|
115 |
res.fl_speed = motors_abs_to_rel(motor_fl_speed, units);
|
|
116 |
res.fr_speed = motors_abs_to_rel(motor_fr_speed, units);
|
|
117 |
res.bl_speed = motors_abs_to_rel(motor_bl_speed, units);
|
|
118 |
res.br_speed = motors_abs_to_rel(motor_br_speed, units);
|
|
78 |
int threshold = 500; //TODO fix after figuring out what sensors give us
|
|
79 |
res.front_raw = front_raw;
|
|
80 |
res.left_raw = left_raw;
|
|
81 |
res.right_raw = back_raw;
|
|
82 |
res.cliff_status = is_cliff;
|
|
119 | 83 |
|
120 |
ROS_DEBUG("Motor speeds queried");
|
|
84 |
ROS_DEBUG("Cliffsensor status queried");
|
|
121 | 85 |
return true; |
122 | 86 |
} |
123 | 87 |
|
124 | 88 |
/** |
125 |
* @brief Converts set speeds (of various units) to absolute speeds. |
|
126 |
* |
|
127 |
* @param speed The speed expressed in the desired units |
|
128 |
* @param units The units the desired speed is measured in |
|
129 |
* @return The absolute speed of the motor |
|
130 |
**/ |
|
131 |
int motors_rel_to_abs(int rel_speed, int units) |
|
132 |
{ |
|
133 |
switch(units) |
|
134 |
{ |
|
135 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
|
136 |
return rel_speed; |
|
137 |
case MOTOR_PERCENT:/* Convert from percentage */ |
|
138 |
return rel_speed * MAXSPEED / 100; |
|
139 |
case MOTOR_MMS:/* Convert from mm/s */ |
|
140 |
/** @todo Make math to do this conversion **/ |
|
141 |
return rel_speed; |
|
142 |
case MOTOR_CMS:/* Convert from cm/s */ |
|
143 |
/** @todo Make math to do this conversion **/ |
|
144 |
return rel_speed; |
|
145 |
default: /* The units aren't recognized */ |
|
146 |
/** @todo Decide on default case. Either percent or absolute. **/ |
|
147 |
return rel_speed; |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* @brief Convert absolute speeds to speeds of various units. |
|
153 |
* |
|
154 |
* @param speed The speed expressed in absolute units. |
|
155 |
* @param units The units the desired speed is measured in. |
|
156 |
* @return The relative speed of the motor in desired units. |
|
157 |
**/ |
|
158 |
int motors_abs_to_rel(int abs_speed, int units) |
|
159 |
{ |
|
160 |
switch(units) |
|
161 |
{ |
|
162 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
|
163 |
return abs_speed; |
|
164 |
case MOTOR_PERCENT:/* Convert from percentage */ |
|
165 |
return abs_speed * 100 / MAXSPEED; |
|
166 |
case MOTOR_MMS:/* Convert from mm/s */ |
|
167 |
/** @todo Make math to do this conversion **/ |
|
168 |
return abs_speed; |
|
169 |
case MOTOR_CMS:/* Convert from cm/s */ |
|
170 |
/** @todo Make math to do this conversion **/ |
|
171 |
return abs_speed; |
|
172 |
default: /* The units aren't recognized */ |
|
173 |
/** @todo Decide on default case. Either percent or absolute. **/ |
|
174 |
return abs_speed; |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
/** |
|
179 |
* @brief Motors driver. This is a ROS node that controls motor speeds. |
|
89 |
* @brief Cliffsensor driver. This is a ROS node that controls cliffsensor status. |
|
180 | 90 |
* |
181 |
* This is the main function for the motors node. It is run when the node
|
|
182 |
* starts and initializes the motors. It then subscribes to the
|
|
183 |
* set_motors, and set_motor_speeds topics, and advertises the
|
|
184 |
* query_motors service.
|
|
91 |
* This is the main function for the cliffsensors node. It is run when the node
|
|
92 |
* starts and initializes the cliffsensors. It then subscribes to the
|
|
93 |
* cliff_status_changed topics, and advertises the
|
|
94 |
* query_cliff service.
|
|
185 | 95 |
* |
186 | 96 |
* @param argc The number of command line arguments (should be 1) |
187 | 97 |
* @param argv The array of command line arguments |
188 | 98 |
**/ |
189 | 99 |
int main(int argc, char **argv) |
190 | 100 |
{ |
191 |
/* Initialize in ROS the motors driver node */
|
|
192 |
ros::init(argc, argv, "motors_driver");
|
|
101 |
/* Initialize in ROS the cliffsensor driver node */
|
|
102 |
ros::init(argc, argv, "cliffsensor_driver");
|
|
193 | 103 |
|
194 |
/* Advertise that this serves the query_motors service */
|
|
104 |
/* Advertise that this serves the query_cliff service */
|
|
195 | 105 |
ros::NodeHandle n; |
196 |
ros::ServiceServer service = n.advertiseService("query_motors",
|
|
197 |
motors_query);
|
|
106 |
ros::ServiceServer service = n.advertiseService("query_cliff",
|
|
107 |
cliff_query);
|
|
198 | 108 |
|
199 |
/* Subscribe to the set_motors topic */ |
|
200 |
ros::Subscriber sub0 = n.subscribe("set_motors", 4, motors_set); |
|
109 |
/* Subscribe to the cliff_status_changed topic */ |
|
110 |
ros::Subscriber sub0 = n.subscribe("cliff_status_changed", |
|
111 |
4, changed_cliff_status); |
|
201 | 112 |
|
202 |
/* Initialize hardware for motors */
|
|
113 |
/* Initialize hardware for cliffsensors */
|
|
203 | 114 |
// Hardware init functions here |
204 | 115 |
|
205 |
ROS_INFO("Ready to set motors.");
|
|
116 |
ROS_INFO("Ready to query cliffsensors.");
|
|
206 | 117 |
ros::spin(); |
207 | 118 |
|
208 | 119 |
return 0; |
Also available in: Unified diff