Skip to content

Commit

Permalink
[#211] Adjust code based on Peter's request
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-glazer committed Feb 15, 2020
1 parent 0ab21c3 commit 57ade01
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions robot/basestation/static/js/roslibjs/ros-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ROSWARN = 4 // warning level
const ROSERROR = 8 // error level
const ROSFATAL = 16 // fatal/critical level

//
function initRosWeb () {
ros = new ROSLIB.Ros({
url: 'ws://' + env.HOST_IP + ':9090'
Expand Down Expand Up @@ -205,18 +206,28 @@ function initRosWeb () {

/* functions used in main code */

/**
* Gets the current time and formats it for ROS and the GUI.
* @return array of two variables: ROS time object, string containing hh:mm:ss
*/
function rosTimestamp () {
// next few lines taken and adjusted from roslibjs action server example
let currentTime = new Date()
let secs = currentTime.getTime()/1000 // seconds before truncating the decimal
let secsFloored = Math.floor(secs) // seconds after truncating
let nsecs = Math.round(1000000000*(secs-secsFloored)) // nanoseconds since the previous second
let nanoSecs = Math.round(1000000000*(secs-secsFloored)) // nanoseconds since the previous second

// return a dictionary for the ROS log and a string for the gui console
let stampTime = currentTime.toString().split(' ')[4] // hh:mm:ss from date object
return [{secs : secsFloored, nsecs : nsecs}, stampTime]
return [{secs : secsFloored, nsecs : nanoSecs}, stampTime]
}

/**
* Creates and publishes a ROS log message to /rosout based on the parameters
* @param logLevel one of the ROS loglevel constants defined above
* @param timestamp ros time object containing seconds and nanoseconds
* @param message the log message
*/
function publishRosLog (logLevel, timestamp, message) {
ros_logger.publish(
new ROSLIB.Message({
Expand All @@ -237,39 +248,74 @@ function publishRosLog (logLevel, timestamp, message) {
)
}

/**
* Prints the log message to the GUI and chrome console.
* Also calls publishRosLog with the message and parameters.
* @param logLevel one of the ROS loglevel constants defined above
* @param message the log message
*/
function rosLog (logLevel, message) {
logData = {}
logData[ROSDEBUG] = {prefix : '[DEBUG]', isError : false}
logData[ROSINFO] = {prefix : '[INFO]', isError : false}
logData[ROSWARN] = {prefix : '[WARN]', isError : true}
logData[ROSERROR] = {prefix : '[ERROR]', isError : true}
logData[ROSFATAL] = {prefix : '[FATAL]', isError : true}
logData[ROSDEBUG] = {prefix : '[DEBUG]', type : 'log'}
logData[ROSINFO] = {prefix : '[INFO]', type : 'log'}
logData[ROSWARN] = {prefix : '[WARN]', type : 'warn'}
logData[ROSERROR] = {prefix : '[ERROR]', type : 'error'}
logData[ROSFATAL] = {prefix : '[FATAL]', type : 'error'}

stamps = rosTimestamp()
consoleMsg = logData[logLevel].prefix + ' [' + stamps[1] + ']: ' + message

!logData[logLevel].isError
? console.log(consoleMsg)
: console.error(consoleMsg)
if (logData[logLevel].type === 'log') {
console.log(consoleMsg)
} else if (logData[logLevel].type === 'warn') {
console.warn(consoleMsg)
} else if (logData[logLevel].type === 'error') {
console.error(consoleMsg)
}
appendToConsole(consoleMsg, false)
// log messages go to log file: currently rosout.log
publishRosLog(logLevel, stamps[0], message)
}

// these functions copy the rospy logging functions
/**
* Sends a debug message with timestamp to the GUI and chrome consoles.
* Also publishes it to /rosout.
* @param message the log message
*/
function logDebug (message) {
// unlike rospy, (currently) the debug messages we generate will get published
rosLog(ROSDEBUG, message)
}
/**
* Sends an info message with timestamp to the GUI and chrome consoles.
* Also publishes it to /rosout.
* @param message the log message
*/
function logInfo (message) {
rosLog(ROSINFO, message)
}
/**
* Sends a warning message with timestamp to the GUI and chrome consoles.
* Also publishes it to /rosout.
* @param message the log message
*/
function logWarn (message) {
rosLog(ROSWARN, message)
}
/**
* Sends an error message with timestamp to the GUI and chrome consoles.
* Also publishes it to /rosout.
* @param message the log message
*/
function logErr (message) {
rosLog(ROSERROR, message)
}
/**
* Sends a fatal error message with timestamp to the GUI and chrome consoles.
* Also publishes it to /rosout.
* @param message the log message
*/
function logFatal (message) {
rosLog(ROSFATAL, message)
}
Expand Down

0 comments on commit 57ade01

Please sign in to comment.