Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure the player is in the vehicle before running rosPubMsg #29

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions modROS.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ function ModROS:update(dt)

if self.doPubMsg then
-- avoid writing to the pipe if it isn't actually open
if self.file_pipe then
-- avoid publishing data if one is not inside a vehicle
if self.file_pipe and g_currentMission.controlledVehicle ~= nil then
self:publish_sim_time_func()
self:publish_veh_func()
self:publish_laser_scan_func()
Expand Down Expand Up @@ -507,37 +508,43 @@ function ModROS:rosPubMsg(flag)
return
end

-- raycastNode initialization
local vehicle = g_currentMission.controlledVehicle
self.instance_veh = VehicleCamera:new(vehicle, ModROS)
local xml_path = self.instance_veh.vehicle.configFileName
local xmlFile = loadXMLFile("vehicle", xml_path)
-- index 0 is outdoor camera; index 1 is indoor camera
-- local cameraKey = string.format("vehicle.enterable.cameras.camera(%d)", 0)

-- get the cameraRaycast node 2(on top of ) which is 0 index .raycastNode(0)
-- get the cameraRaycast node 3 (in the rear) which is 1 index .raycastNode(1)

local cameraKey = string.format("vehicle.enterable.cameras.camera(%d).raycastNode(0)", 0)
XMLUtil.checkDeprecatedXMLElements(xmlFile, xml_path, cameraKey .. "#index", "#node") -- FS17 to FS19
local camIndexStr = getXMLString(xmlFile, cameraKey .. "#node")
self.instance_veh.cameraNode =
I3DUtil.indexToObject(
self.instance_veh.vehicle.components,
camIndexStr,
self.instance_veh.vehicle.i3dMappings
)
if self.instance_veh.cameraNode == nil then
print("nil camera")
-- else
-- print(instance_veh.cameraNode)
-- if the player is not in the vehicle, print error and return
if not vehicle then
Copy link
Member

@gavanderhoorn gavanderhoorn Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will avoid the nil related error, but should self.rosPubMsg also not be reset to false in this case?

Otherwise I believe ModROS:update(..) will try to still publish data, right?

Here:

FS19_modROS/modROS.lua

Lines 104 to 119 in cf452a6

function ModROS:update(dt)
-- self.dt = self.dt + dt
-- create TFMessage object
self.tf_msg = tf2_msgs_TFMessage:init()
if self.doPubMsg then
-- avoid writing to the pipe if it isn't actually open
if self.file_pipe then
self:publish_sim_time_func()
self:publish_veh_func()
self:publish_laser_scan_func()
self:publish_imu_func()
self:publish_tf()
end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in that case we should probably also guard the opening of the pipe file, otherwise that will be attempted multiple times.

Copy link
Contributor Author

@tckarenchiang tckarenchiang Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But 0f5e401 also avoids publishing data if the player is not in a vehicle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why even continue with rosPubMsg(..) if it's clear the player is not in a vehicle?

By adding more conditionals everywhere it looks like we're just complicating things which could be kept simple.

Copy link
Contributor Author

@tckarenchiang tckarenchiang Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because initially I wanted to control the inactive vehicles as well, as mentioned in #3. but yes, let's fix the current issue for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So are you planning to change anything, or should we merge this version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we should just merge this version for now. Even though it's not a proper fix, it does solve the issue I believe.

print("You are not inside any vehicle, come on! Enter 'e' to hop in one next to you!")
return
else
self.instance_veh = VehicleCamera:new(vehicle, ModROS)
local xml_path = self.instance_veh.vehicle.configFileName
local xmlFile = loadXMLFile("vehicle", xml_path)
-- index 0 is outdoor camera; index 1 is indoor camera
-- local cameraKey = string.format("vehicle.enterable.cameras.camera(%d)", 0)

-- get the cameraRaycast node 2(on top of ) which is 0 index .raycastNode(0)
-- get the cameraRaycast node 3 (in the rear) which is 1 index .raycastNode(1)

local cameraKey = string.format("vehicle.enterable.cameras.camera(%d).raycastNode(0)", 0)
XMLUtil.checkDeprecatedXMLElements(xmlFile, xml_path, cameraKey .. "#index", "#node") -- FS17 to FS19
local camIndexStr = getXMLString(xmlFile, cameraKey .. "#node")
self.instance_veh.cameraNode =
I3DUtil.indexToObject(
self.instance_veh.vehicle.components,
camIndexStr,
self.instance_veh.vehicle.i3dMappings
)
if self.instance_veh.cameraNode == nil then
print("nil camera")
-- else
-- print(instance_veh.cameraNode)
end
-- create self.laser_frame_1 attached to raycastNode (x left, y up, z into the page)
-- and apply a transform to the self.laser_frame_1
local tran_x, tran_y, tran_z = mod_config.laser_scan.laser_transform.translation.x, mod_config.laser_scan.laser_transform.translation.y, mod_config.laser_scan.laser_transform.translation.z
local rot_x, rot_y, rot_z = mod_config.laser_scan.laser_transform.rotation.x, mod_config.laser_scan.laser_transform.rotation.y, mod_config.laser_scan.laser_transform.rotation.z
self.laser_frame_1 = frames.create_attached_node(self.instance_veh.cameraNode, "self.laser_frame_1", tran_x, tran_y, tran_z, rot_x, rot_y, rot_z)
end

-- create self.laser_frame_1 attached to raycastNode (x left, y up, z into the page)
-- and apply a transform to the self.laser_frame_1
local tran_x, tran_y, tran_z = mod_config.laser_scan.laser_transform.translation.x, mod_config.laser_scan.laser_transform.translation.y, mod_config.laser_scan.laser_transform.translation.z
local rot_x, rot_y, rot_z = mod_config.laser_scan.laser_transform.rotation.x, mod_config.laser_scan.laser_transform.rotation.y, mod_config.laser_scan.laser_transform.rotation.z
self.laser_frame_1 = frames.create_attached_node(self.instance_veh.cameraNode, "self.laser_frame_1", tran_x, tran_y, tran_z, rot_x, rot_y, rot_z)

elseif flag == nil or flag == "" or flag == "false" then
self.doPubMsg = false
print("stop publishing data, set true, if you want to publish Pose")
Expand Down