-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathParam.js
More file actions
127 lines (117 loc) · 3.73 KB
/
Param.js
File metadata and controls
127 lines (117 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @fileOverview
* @author Brandon Alexander - baalexander@gmail.com
*/
import Service from './Service.js';
import Ros from '../core/Ros.js';
/**
* A ROS parameter.
*/
export default class Param {
/**
* @param {Object} options
* @param {Ros} options.ros - The ROSLIB.Ros connection handle.
* @param {string} options.name - The param name, like max_vel_x.
* @param {'int' | 'double'} [options.numberTypeHint] - The type hint for numbers. Defaults to 'int'.
*/
constructor(options) {
this.ros = options.ros;
this.name = options.name;
this.numberTypeHint = options.numberTypeHint ?? 'int';
}
/**
* Function to replace or transform values during JSON stringification.
*
* Stringification of a whole double value will result in a string without a decimal point.
* (e.g. 1.0 will be stringified as "1"). This causes conflict with ROS2 parameters, which incorrectly parse the incoming value as an integer.
* If dynamic typing is not enabled, this will cause the parameter set operation to fail.
*
* @param {string} _key - The key of the property being replaced (not used in the logic but needed for a replacer).
* @param {unknown} value - The value to be inspected or transformed.
* @returns {unknown} - The original value or a transformed representation of the value.
*/
replacer = (_key, value) => {
if (typeof value === 'number' && this.numberTypeHint === 'double') {
let numberStr = String(value);
if (!numberStr.includes('.')) {
numberStr += '.0';
}
return numberStr;
}
return value;
};
/**
* @callback getCallback
* @param {Object} value - The value of the param from ROS.
*/
/**
* @callback getFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Fetch the value of the param.
*
* @param {getCallback} callback - The callback function.
* @param {getFailedCallback} [failedCallback] - The callback function when the service call failed.
*/
get(callback, failedCallback) {
var paramClient = new Service({
ros: this.ros,
name: 'rosapi/get_param',
serviceType: 'rosapi/GetParam'
});
var request = {name: this.name};
paramClient.callService(
request,
function (result) {
var value = JSON.parse(result.value);
callback(value);
},
failedCallback
);
}
/**
* @callback setParamCallback
* @param {Object} response - The response from the service request.
*/
/**
* @callback setParamFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Set the value of the param in ROS.
*
* @param {Object} value - The value to set param to.
* @param {setParamCallback} [callback] - The callback function.
* @param {setParamFailedCallback} [failedCallback] - The callback function when the service call failed.
*/
set(value, callback, failedCallback) {
var paramClient = new Service({
ros: this.ros,
name: 'rosapi/set_param',
serviceType: 'rosapi/SetParam'
});
var request = {
name: this.name,
value: JSON.stringify(value, this.replacer)
};
paramClient.callService(request, callback, failedCallback);
}
/**
* Delete this parameter on the ROS server.
*
* @param {setParamCallback} callback - The callback function.
* @param {setParamFailedCallback} [failedCallback] - The callback function when the service call failed.
*/
delete(callback, failedCallback) {
var paramClient = new Service({
ros: this.ros,
name: 'rosapi/delete_param',
serviceType: 'rosapi/DeleteParam'
});
var request = {
name: this.name
};
paramClient.callService(request, callback, failedCallback);
}
}