Serializers

abb_rws_client_python_rw6.core.serializers

Serialization / deserialization of RAPID types ↔ RWS format.

Author: Clement RACINET

RWS format for a robtarget (compact string, no spaces)::

[[x,y,z],[q1,q2,q3,q4],[cf1,cf4,cf6,cfx],[eax_a,eax_b,eax_c,eax_d,eax_e,eax_f]]

ABB quaternion convention : [w, x, y, z] (scalar first) Inactive external axis : 9E+9

RobTarget dataclass

Python representation of an ABB robtarget.

Attributes:
  • x (float) –

    Cartesian X position (mm).

  • y (float) –

    Cartesian Y position (mm).

  • z (float) –

    Cartesian Z position (mm).

  • qw (float) –

    Orientation quaternion — scalar component (ABB convention: scalar first).

  • qx (float) –

    Orientation quaternion — X component.

  • qy (float) –

    Orientation quaternion — Y component.

  • qz (float) –

    Orientation quaternion — Z component.

  • cf1 (float) –

    Robot configuration — axis 1 quadrant.

  • cf4 (float) –

    Robot configuration — axis 4 quadrant.

  • cf6 (float) –

    Robot configuration — axis 6 quadrant.

  • cfx (float) –

    Robot configuration — extended quadrant.

  • eax (list[float]) –

    External axes (exactly 6 values; use 9E+9 for inactive axes).

Raises:
Example
>>> rt = RobTarget(x=100.0, y=200.0, z=300.0)
>>> rt.eax
[9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0]
Source code in abb_rws_client_python_rw6/core/serializers.py
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
@dataclass
class RobTarget:
    """Python representation of an ABB robtarget.

    Attributes:
        x: Cartesian X position (mm).
        y: Cartesian Y position (mm).
        z: Cartesian Z position (mm).
        qw: Orientation quaternion — scalar component (ABB convention: scalar first).
        qx: Orientation quaternion — X component.
        qy: Orientation quaternion — Y component.
        qz: Orientation quaternion — Z component.
        cf1: Robot configuration — axis 1 quadrant.
        cf4: Robot configuration — axis 4 quadrant.
        cf6: Robot configuration — axis 6 quadrant.
        cfx: Robot configuration — extended quadrant.
        eax: External axes (exactly 6 values; use ``9E+9`` for inactive axes).

    Raises:
        RWSValueError: If ``eax`` does not contain exactly 6 values.

    Example:
        ```python
        >>> rt = RobTarget(x=100.0, y=200.0, z=300.0)
        >>> rt.eax
        [9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0, 9000000000.0]
        ```
    """

    x: float = 0.0
    y: float = 0.0
    z: float = 0.0
    qw: float = 1.0
    qx: float = 0.0
    qy: float = 0.0
    qz: float = 0.0
    cf1: float = 0.0
    cf4: float = 0.0
    cf6: float = 0.0
    cfx: float = 0.0
    eax: list[float] = field(default_factory=lambda: [_INACTIVE_AXIS] * 6)

    def __post_init__(self) -> None:
        if len(self.eax) != 6:
            raise RWSValueError(f"eax must have exactly 6 values, got {len(self.eax)}")

python_to_rapid_value(value, rapid_type)

Convert a Python value to a RAPID string for the RWS API.

Parameters:
  • value (float | bool | str | RobTarget) –

    Python value to convert.

  • rapid_type (str) –

    Target RAPID type: "num", "bool", "string", or "robtarget".

Returns:
  • str –

    String in the format expected by RWS.

Raises:
  • RWSValueError –

    Unknown RAPID type, or value incompatible with the requested type.

Example
>>> python_to_rapid_value(3.14, "num")
'3.14'
>>> python_to_rapid_value(True, "bool")
'TRUE'
>>> python_to_rapid_value("hello", "string")
'hello'
Source code in abb_rws_client_python_rw6/core/serializers.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
def python_to_rapid_value(value: float | bool | str | RobTarget, rapid_type: str) -> str:
    """Convert a Python value to a RAPID string for the RWS API.

    Args:
        value: Python value to convert.
        rapid_type: Target RAPID type: ``"num"``, ``"bool"``,
            ``"string"``, or ``"robtarget"``.

    Returns:
        String in the format expected by RWS.

    Raises:
        RWSValueError: Unknown RAPID type, or value incompatible
            with the requested type.

    Example:
        ```python
        >>> python_to_rapid_value(3.14, "num")
        '3.14'
        >>> python_to_rapid_value(True, "bool")
        'TRUE'
        >>> python_to_rapid_value("hello", "string")
        'hello'
        ```
    """
    if rapid_type not in _RAPID_TYPES:
        raise RWSValueError(
            f"Unknown RAPID type: {rapid_type!r}. Expected one of {sorted(_RAPID_TYPES)}"
        )
    if rapid_type == "num":
        # bool is a subclass of int — must be rejected explicitly
        if isinstance(value, bool) or not isinstance(value, int | float):
            raise RWSValueError(f"Expected int or float for 'num', got {type(value).__name__!r}")
        return str(int(value)) if float(value) == int(value) else repr(float(value))
    if rapid_type == "bool":
        if not isinstance(value, bool):
            raise RWSValueError(f"Expected bool for 'bool', got {type(value).__name__!r}")
        return "TRUE" if value else "FALSE"
    if rapid_type == "string":
        if not isinstance(value, str):
            raise RWSValueError(f"Expected str for 'string', got {type(value).__name__!r}")
        return value  # no quotes: RWS handles encoding on the form-data side
    if rapid_type == "robtarget":
        if not isinstance(value, RobTarget):
            raise RWSValueError(f"Expected RobTarget for 'robtarget', got {type(value).__name__!r}")
        return robtarget_to_rws(value)
    raise RWSValueError(f"Unknown RAPID type: {rapid_type!r}")  # unreachable — mypy guard

rapid_value_to_python(raw, rapid_type)

Convert a raw RWS string to a Python value according to the RAPID type.

Parameters:
  • raw (str) –

    Raw string returned by the RWS controller.

  • rapid_type (str) –

    RAPID type: "num", "bool", "string", or "robtarget".

Returns:
  • float | bool | str | RobTarget –

    Typed Python value.

Raises:
Example
>>> rapid_value_to_python("3.14", "num")
3.14
>>> rapid_value_to_python("TRUE", "bool")
True
>>> rapid_value_to_python("hello", "string")
'hello'
Source code in abb_rws_client_python_rw6/core/serializers.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def rapid_value_to_python(raw: str, rapid_type: str) -> float | bool | str | RobTarget:
    """Convert a raw RWS string to a Python value according to the RAPID type.

    Args:
        raw: Raw string returned by the RWS controller.
        rapid_type: RAPID type: ``"num"``, ``"bool"``,
            ``"string"``, or ``"robtarget"``.

    Returns:
        Typed Python value.

    Raises:
        RWSValueError: Conversion failed or unknown RAPID type.

    Example:
        ```python
        >>> rapid_value_to_python("3.14", "num")
        3.14
        >>> rapid_value_to_python("TRUE", "bool")
        True
        >>> rapid_value_to_python("hello", "string")
        'hello'
        ```
    """
    if rapid_type not in _RAPID_TYPES:
        raise RWSValueError(
            f"Unknown RAPID type: {rapid_type!r}. Expected one of {sorted(_RAPID_TYPES)}"
        )
    if rapid_type == "num":
        try:
            return float(raw)
        except ValueError as exc:
            raise RWSValueError(f"Cannot convert {raw!r} to num") from exc
    if rapid_type == "bool":
        normalized = raw.strip().lower()
        if normalized in {"true", "1", "yes"}:
            return True
        if normalized in {"false", "0", "no"}:
            return False
        raise RWSValueError(f"Cannot convert {raw!r} to bool")
    if rapid_type == "string":
        return raw  # raw passthrough, no quote stripping
    if rapid_type == "robtarget":
        return rws_to_robtarget(raw)
    raise RWSValueError(f"Unknown RAPID type: {rapid_type!r}")  # unreachable — mypy guard

robtarget_to_rws(rt)

Serialize a RobTarget into a compact RWS string.

Route: PUT /rw/rapid/symbol/data/RAPID/{task}/{module}/{symbol}

Parameters:
  • rt (RobTarget) –

    RobTarget instance to serialize.

Returns:
  • str –

    Compact RWS string with no spaces, e.g.:

  • str –

    "[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]"

Example
>>> robtarget_to_rws(RobTarget(x=100.0, y=200.0, z=300.0))
'[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]'
Source code in abb_rws_client_python_rw6/core/serializers.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def robtarget_to_rws(rt: RobTarget) -> str:
    """Serialize a RobTarget into a compact RWS string.

    Route: ``PUT /rw/rapid/symbol/data/RAPID/{task}/{module}/{symbol}``

    Args:
        rt: RobTarget instance to serialize.

    Returns:
        Compact RWS string with no spaces, e.g.:
        ``"[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]"``

    Example:
        ```python
        >>> robtarget_to_rws(RobTarget(x=100.0, y=200.0, z=300.0))
        '[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]'
        ```
    """
    trans = ",".join(_fmt(v) for v in (rt.x, rt.y, rt.z))
    rot = ",".join(_fmt(v) for v in (rt.qw, rt.qx, rt.qy, rt.qz))
    conf = ",".join(_fmt(v) for v in (rt.cf1, rt.cf4, rt.cf6, rt.cfx))
    ext = ",".join(_fmt(v) for v in rt.eax)
    return f"[[{trans}],[{rot}],[{conf}],[{ext}]]"

rws_to_robtarget(raw)

Deserialize a raw RWS string into a RobTarget instance.

Route: GET /rw/rapid/symbol/data/RAPID/{task}/{module}/{symbol}

Parameters:
  • raw (str) –

    Raw RWS string returned by the controller.

Returns:
  • RobTarget –

    Populated RobTarget instance.

Raises:
  • RWSValueError –

    If the string format is invalid or any value cannot be parsed as a float.

Example
>>> rws_to_robtarget(
...     "[[0,0,500],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]"
... )
RobTarget(x=0.0, y=0.0, z=500.0, ...)
Source code in abb_rws_client_python_rw6/core/serializers.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def rws_to_robtarget(raw: str) -> RobTarget:
    """Deserialize a raw RWS string into a RobTarget instance.

    Route: ``GET /rw/rapid/symbol/data/RAPID/{task}/{module}/{symbol}``

    Args:
        raw: Raw RWS string returned by the controller.

    Returns:
        Populated RobTarget instance.

    Raises:
        RWSValueError: If the string format is invalid or any value
            cannot be parsed as a float.

    Example:
        ```python
        >>> rws_to_robtarget(
        ...     "[[0,0,500],[1,0,0,0],[0,0,0,0],[9E+9,9E+9,9E+9,9E+9,9E+9,9E+9]]"
        ... )
        RobTarget(x=0.0, y=0.0, z=500.0, ...)
        ```
    """
    m = _ROBTARGET_RE.match(raw.strip())
    if not m:
        raise RWSValueError(f"Invalid robtarget string: {raw!r}")
    trans = _parse_floats(m.group(1), 3, "trans")
    rot = _parse_floats(m.group(2), 4, "rot")
    conf = _parse_floats(m.group(3), 4, "conf")
    ext = _parse_floats(m.group(4), 6, "eax")
    return RobTarget(
        x=trans[0],
        y=trans[1],
        z=trans[2],
        qw=rot[0],
        qx=rot[1],
        qy=rot[2],
        qz=rot[3],
        cf1=conf[0],
        cf4=conf[1],
        cf6=conf[2],
        cfx=conf[3],
        eax=ext,
    )