nh.loginfo("request: x=%ld, y=%ld",(long int)req.a,(long int)req.b);c++显示只需要一个参数
时间: 2023-07-16 12:17:09 浏览: 135
是的,C++中的printf和类似函数可以使用格式化字符串来指定输出的格式,其中%ld表示输出一个长整型数字。
而在C++中,如果你想在字符串中插入变量,可以使用字符串流(stringstream)来实现。你可以这样写:
```
#include <sstream>
// ...
std::stringstream ss;
ss << "request: x=" << req.a << ", y=" << req.b;
nh.loginfo(ss.str().c_str());
```
这里,我们将请求的参数和字符串拼接在一起,并将结果转换为一个C字符串,以便能够传递给loginfo函数。
相关问题
nh.loginfo("request: x=%ld, y=%ld",(long int)req.a,(long int)req.b);显示只需要一个参数
是的,`nh.loginfo()`函数只需要一个参数作为日志消息的格式化字符串,如果需要将多个参数格式化成字符串,可以使用Python的字符串格式化功能。例如,将两个整数变量`x`和`y`格式化成一个字符串:
```
nh.loginfo("request: x=%ld, y=%ld" % ((long int)req.a, (long int)req.b))
```
或者使用更现代的字符串格式化语法:
```
nh.loginfo("request: x={}, y={}".format(req.a, req.b))
```
这两种方法都可以将`req.a`和`req.b`的值格式化成字符串,并插入到日志消息中。
更正这个Python代码import rospy from mavros_msgs.msg import State from mavros_msgs.srv import CommandBool, SetMode from geometry_msgs.msg import PoseStamped import time current_state = State() def state_cb(msg): global current_state current_state = msg rospy.init_node('position') rate = rospy.Rate(20.0) state_sub = rospy.Subscriber("mavros/state", State, state_cb) local_pos_pub = rospy.Publisher("mavros/setpoint_position/local", PoseStamped, queue_size=10) arming_client = rospy.ServiceProxy("mavros/cmd/arming", CommandBool) set_mode_client = rospy.ServiceProxy("mavros/set_mode", SetMode) wait for FCU connection while not rospy.is_shutdown() and not current_state.connected: rate.sleep() pose = PoseStamped() pose.pose.position.x = 0 pose.pose.position.y = 0 pose.pose.position.z = 1.5 offb_set_mode = SetMode() offb_set_mode.custom_mode = "OFFBOARD" arm_cmd = CommandBool() arm_cmd.value = True state = 3 last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): if not current_state.armed: if arming_client(arm_cmd) and arm_cmd.response.success: rospy.loginfo("Vehicle armed") if current_state.mode != "OFFBOARD": if set_mode_client(offb_set_mode) and offb_set_mode.response.mode_sent: rospy.loginfo("Offboard enabled") rate.sleep() while state > 0: last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): pose.pose.position.x = 0 pose.pose.position.y = 0 local_pos_pub.publish(pose) rospy.loginfo("SUCCESS0") rate.sleep() last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): pose.pose.position.x = 2 pose.pose.position.y = 2 local_pos_pub.publish(pose) rospy.loginfo("SUCCESS1") rate.sleep() state -= 1 rospy.loginfo("state=" + str(state)) offb_set_mode.custom_mode = "AUTO.LAND" if set_mode_client(offb_set_mode) and offb_set_mode.response.mode_sent: rospy.loginfo("AUTO.LAND enabled") last_request = rospy.Time.now() rospy.spin()
这段 Python 代码主要是控制飞行器执行一定的动作。代码中,首先通过导入 rospy、mavros_msgs.msg、mavros_msgs.srv、geometry_msgs.msg 等模块,定义了一些变量和函数。
然后,在 while 循环中,程序会等待与飞控系统的连接建立,之后会设置飞行器的起飞高度为 1.5 米,然后进入一个 while 循环,其中会执行飞行器的上升和向右移动,最后执行自动降落的任务。
其中,offb_set_mode.custom_mode = "OFFBOARD" 会将飞行器的模式设置为 OFFBOARD,arm_cmd.value = True 会将飞行器的电机解锁,local_pos_pub.publish(pose) 会将飞行器的位置信息发布到话题中,set_mode_client(offb_set_mode) 会将飞行器的模式切换为指定的模式,arming_client(arm_cmd) 会将飞行器的电机锁定或解锁。
阅读全文