旋转传感器#

介绍#

The rotation class is used to control and access data from a V5 Rotation Sensor.

The Rotation Sensor measures the rotation of a shaft. It can be used anywhere on a robot where a shaft is rotating, even if that shaft is not connected directly to a motor.

A motor has a built-in encoder that can track its own rotation. The Rotation Sensor works like an external encoder that can track the rotation of a separate shaft.

The Rotation Sensor can return the shaft’s angle, position, and velocity. Angle is the shaft’s absolute position from 0 to 359.99 degrees. It does not reset when a project starts or when the robot is powered off. Position tracks how many degrees or turns the shaft has rotated forward or reverse during a project, and it can be reset as needed.

The Rotation Sensor is compatible with 1/8 inch and 1/4 inch standard VEX shafts.

类构造函数#

rotation(
    int32_t index,
    bool    reverse = false );

类析构函数#

Destroys the rotation object and releases associated resources.

virtual ~rotation();

参数#

范围

类型

描述

index

int32_t

The Smart Port that the Rotation Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1).

reverse

bool

Optional. Whether to reverse the Rotation Sensor output:

  • true — Reverses the direction of the reported angle and position values.
  • false (default) — Reports angle and position values in the standard direction.

示例#

// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);

成员功能#

The rotation class includes the following member functions:

  • setPosition — Sets the Rotation Sensor’s current position to a specific value.

  • angle — Returns the shaft’s current angle from 0 to 359.99 degrees.

  • position — Returns how far the shaft has rotated in degrees or turns.

  • velocity — Returns how fast the shaft is rotating.

Before calling any rotation member functions, a rotation instance must be created, as shown below:

// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);

setPosition#

Sets the Rotation Sensor’s current position to a specific value.

Position is how far the shaft has rotated during a project. Setting the position changes the current position value without moving the shaft.

Available Functions
void setPosition(
    double        value,
    rotationUnits units );

Parameters

范围

类型

描述

value

double

The position value to assign to the Rotation Sensor. This can be an integer or a decimal.

units

rotationUnits

The position unit:

  • deg / degrees — degrees
  • turns / rev — turns

Return Values

此函数不返回值。

angle#

Returns the shaft’s current angle.

Angle reports the shaft’s absolute position within one rotation, from 0 to 359.99 degrees. It does not reset when a project starts or when the robot is powered off.

Available Functions
double angle( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

Optional. The angle unit:

  • deg / degrees (default) — degrees
  • turns / rev — turns

Return Values

Returns a double representing the shaft’s current angle in the specified units.

Examples
// Get the current angle of the shaft
  double angle = Rotation1.angle(degrees);

  // Print the current angle of the shaft to the
  // Brain's screen.
  Brain.Screen.print(angle);

position#

Returns how far the shaft has rotated.

Unlike angle, position can increase past 359.99 degrees or decrease below 0 degrees. Position tracks how far the shaft rotates forward or reverse during a project and can be reset as needed.

Available Functions
double position( rotationUnits units );

Parameters

范围

类型

描述

units

rotationUnits

The position unit:

  • deg / degrees — degrees
  • turns / rev — turns

Return Values

Returns a double representing how far the shaft has rotated in the specified units.

velocity#

Returns how fast the shaft is rotating.

Available Functions
double velocity( velocityUnits units );

Parameters

范围

类型

描述

units

velocityUnits

The velocity unit:

  • rpm — revolutions per minute
  • dps — degrees per second

Return Values

Returns a double representing how fast the shaft is rotating in the specified units.