Skip to content

set_velocities raises ValueError when ids is a scalar or 0d array #39

Description

@pooleya19

Calling set_velocities(ids, velocities) raises a ValueError when ids is a scalar or a 0-dimensional NDArray, which might be used for single-robot control.

Reproduction (Single robot moving in a circle):

import numpy as np
from rps.robotarium import Robotarium

robotarium = Robotarium(number_of_robots=1, show_figure=True)
for _ in range(100):
    poses = robotarium.get_poses()
    command = np.array([[0.2], [1]])
    robotarium.set_velocities(0, command)                       # crashes
    # robotarium.set_velocities(np.array(0), command)           # crashes
    # robotarium.set_velocities([0], command)                   # no crash
    # robotarium.set_velocities(np.atleast_1d(0), command)      # no crash
    # robotarium.set_velocities(np.arange(1), command)          # no crash
    robotarium.step()

Error:

ValueError: could not broadcast input array from shape (2,1) into shape (2,)

Cause:

In set_velocities, if ids is a scalar or 0d array, self._velocities[:,ids] has shape (2,). Because velocities is (2,1), numpy fails to broadcast.

Suggested fix:

Hopefully I will follow this with a pull request. My suggested fix would be to make ids be at least 1d, allowing self._velocities[:,ids] to maintain a shape of (2,1). This would have no effect if ids is already 1d or 2d.

ids = np.atleast_1d(ids)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions