gpt4 book ai didi

javascript - 无法通过 Rosbridge 订阅 Odometry ROS 消息

转载 作者:行者123 更新时间:2023-11-28 17:01:39 24 4
gpt4 key购买 nike

目标是让我的形状(即“跟踪器”)模仿我的机器人的运动。我有两个文件,其中一个包含 rosbridge 代码,用于从机器人中提取主题:

listener_Odom = new ROSLIB.Topic ({
ros : ros,
name : '/odom',
messageType : 'nav_msgs/Odometry'
});

let odomPosition = {xPos : 0, yPos : 0};
listener_Odom.subscribe('/odom', function(message){
console.log(`Received message on ${listener_Odom.name} : ${message.data}`);

let odomPosition = {
xPos : message.pose.pose.position.x,
yPos : message.pose.pose.position.y
}

tracker.update(odomPosition);

...并且,在一个单独的文件中获取我的 rosbridge,这段代码用于在前端移动形状:

tracker = new track(30, 50, "white", 100, 820, 0)
function track(width, height, color, distanceX, distanceY, rotation, odomPosition){
this.width = width;
this.height = height;
this.speed = 0;
//this.distanceX = odomPosition.xPos || 0;
//this.distanceY = odomPosition.yPos || 0;
this.rotation = rotation || 0;
this.rotationSpeed = 0;
console.log("inside track()");

this.update = function(odomPosition){
ctx = mainCanvas.ctx1;
ctx.fillStyle = color;
ctx.save();
ctx.translate(odomPosition.xPos, odomPosition.yPos);
ctx.rotate(this.rotation); //rotate diagram specified below:
ctx.fillRect(-this.width/2, -this.height/2, width, height); //first 2 variables ensure that it rotates around its center, and not around origin
ctx.beginPath();
ctx.moveTo(50, 0);
ctx.lineTo(10,25);
ctx.lineTo(10,-25);
ctx.fill();
ctx.restore();
}
this.newPosition = function(){
this.rotation += this.rotationSpeed;
this.distanceX += this.speed * Math.cos(this.rotation); //twist with respect to cosine and sine;
this.distanceY += this.speed * Math.sin(this.rotation);
}
}

function moveTracker(){ //recognize keys from keyboard
mainCanvas.clear();
tracker.speed = 0; //for linear speed
tracker.rotationSpeed = 0; //for angular speed
if (mainCanvas.key && mainCanvas.key == 37) { //left key; countercl. rotation
tracker.rotationSpeed = -.1/ (Math.PI);
rosCmdVel.publish(rosTwistLft);
//console.log('swinging anticlockwise');
}
if (mainCanvas.key && mainCanvas.key == 38) { //up key
tracker.speed = 3;
rosCmdVel.publish(rosTwistFwd);
//console.log('moving forward');
}
if (mainCanvas.key && mainCanvas.key == 39) { //right key; clockw. rotation
tracker.rotationSpeed = .1 / (Math.PI);
rosCmdVel.publish(rosTwistRht);
//console.log('swinging clockwise');
}
if (mainCanvas.key && mainCanvas.key == 40) { //down key
tracker.speed= -3;
rosCmdVel.publish(rosTwistBwd);
//console.log('moving backward');
}
tracker.newPosition();
//tracker.update();
}
  • 如何让我的前端文件正确订阅/odom主题并移动形状?

编辑:我已经完全包含了与移动相关的代码部分。

最佳答案

我相信您的问题出在这段代码中:

listener_Odom.subscribe('/odom', function(message){
console.log(`Received message on ${listener_Odom.name} : ${message.data}`);
OdomPosition = {
xPos : message.pose.pose.position.x,
yPos : message.pose.pose.position.y
}
return OdomPosition;
}
); // minor: I think this line is missing in your pasted code

订阅方法将调用给定的回调,因此从中返回 OdomPosition 并没有真正的意义。您可以将 OdomPosition 视为全局变量,我认为这正是您最初想要做的。像这样的东西:

let OdomPosition = {xPos:0, yPos:0}
listener_Odom.subscribe('/odom', function(message){
console.log(`Received message on ${listener_Odom.name} : ${message.data}`);
OdomPosition.xPos : message.pose.pose.position.x
OdomPosition.yPos : message.pose.pose.position.y
}
);

请注意,您当前的代码会在每次回调时重新创建对象引用。但是,即使这样也存在问题,因为无法保证回调会在您在 moveTracker 处理中调用 tracker.update 之前发生。

我认为您真正想做的另一种选择是在每次 odom 更改时调用 tracker.update。也许是这样的:

listener_Odom.subscribe('/odom', function(message){
console.log(`Received message on ${listener_Odom.name} : ${message.data}`);
let odomPosition = {
xPos : message.pose.pose.position.x,
yPos : message.pose.pose.position.y
}

tracker.update(odomPosition)
}
);

然后修改您的 tracker.update 方法声明以将 odomPosition 作为参数,并在 moveTracker 方法中删除对 tracker.update 的调用。

关于javascript - 无法通过 Rosbridge 订阅 Odometry ROS 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com