Skip to content

Commit

Permalink
Merge pull request #29 from tud-cor/fix_nil_vehicle
Browse files Browse the repository at this point in the history
Make sure the player is in the vehicle before running rosPubMsg
  • Loading branch information
tckarenchiang authored Jun 4, 2021
2 parents 180bdd2 + 2cd5c6a commit 3b2540e
Showing 1 changed file with 36 additions and 29 deletions.
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
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

0 comments on commit 3b2540e

Please sign in to comment.