| 
 | 
 
 
发表于 2019-7-5 21:46:07
 26966 浏览  3 回复
                                 
 
 
[控制算法]
ros订阅的回调函数返回值如何使用
                                
 
3阿木币 
#include<ros/ros.h> 
#include "how_to_move.h" 
 
double get_angle(const nav_msgs::Odometry::ConstPtr &odom){ 
    /*经过一堆算法后,得到返回值angle*/ 
 
 
return angle;#1111111111111111111111111111111111111111111111111 
 
 
} 
 
int main(int argc,char** argv){ 
    ros::init(argc,argv,"how_to_move"); 
    ros::NodeHandle n_move; 
    geometry_msgs::Twist turn_speed = set_turn_speed(10); 
 
 
    ros::Subscriber sub = n_move.subscribe<nav_msgs::Odometry>("/odom",1,get_angle);#2222222222222222222222222222222 
 
 
    ros::Rate loop_rate(10); 
     
    while(ros::ok()){ 
        ros::spinOnce(); 
 
 
        while(<0 || >1){#33333333333333333333333333333333333333333333333 
            pub.publish(turn_speed); 
            loop_rate.sleep(); 
        } 
         
    } 
    return 0; 
} 
 
 
 
 
上面为源代码,在标记一处,我定义了回调函数,返回一个数,标记2处为订阅的定义,标记3处,我想使用回调函数的返回值作为while语句的判定条件,我该怎么使用呢,直接用 
while(angle<0 || angle>1)或者while(get_angle<0 ||get_angle >1)肯定是不行的,大佬是怎么处理回调函数返回值使用问题的呢? 
 
 
 |   
 
 
最佳答案
查看完整内容 
你这不过关啊 入门ros至少先把ros教程的前10讲学一下吧
直接给你帖代码吧 一般用第二种 但你想分开储存 也可用第一种 
 
 
 
 
  扫一扫浏览分享 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
| 
 | 
 
 
发表于 2019-7-5 21:46:08
                                 
 
 
你这不过关啊 入门ros至少先把ros教程的前10讲学一下吧 
 
直接给你帖代码吧 一般用第二种 但你想分开储存 也可用第一种 
 
-  
 
 -         void att_target_cb(const mavros_msgs::AttitudeTarget::ConstPtr& msg)
 
 -         {
 
 -             q_fcu_target = Eigen::Quaterniond(msg->orientation.w, msg->orientation.x, msg->orientation.y, msg->orientation.z);
 
  
-             //Transform the Quaternion to euler Angles
 
 -             euler_fcu_target = quaternion_to_euler(q_fcu_target);
 
  
-             rates_fcu_target = Eigen::Vector3d(msg->body_rate.x, msg->body_rate.y, msg->body_rate.z);
 
  
-             Thrust_target = msg->thrust;
 
 -         }
 
  
-         void actuator_target_cb(const mavros_msgs::ActuatorControl::ConstPtr& msg)
 
 -         {
 
 -             actuator_target = *msg;
 
 -         }
 
  复制代码 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
| 
 | 
 
 楼主|
发表于 2019-7-6 09:56:48
来自手机
                                 
 
 
| 
噢噢,好像明白了,用第二种,是不是要全局定义actuator_target再用? |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
| 
 | 
 
 
发表于 2019-7-6 22:28:02
                                 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 
         
  
         
 |