硬件:vex edr v5主控、轴角编码器、vex edr v5遥控器、超声波传感器
软件:VEX Coding Studio
1、轴角编码器
用途:测量轴转过的角度,转一圈返回360
取值范围:理论上来讲,-∞~+∞
接线:两个引脚接到相邻的接口
比如说,1接到A接口,2接到B接口,那么顺时针旋转,返回值增大,反之,1接到B接口,2接到A接口,顺时针旋转,返回值减小
定义接口
vex::encoder Encoder = vex::encoder(Brain.ThreeWirePort.A);
只需定义一个接口
定义的接口必须是单号的,如A、C、E、G
编码器相关函数
点开灰色圆圈的"i",可以看到该函数的详细信息描述
设置类
功能:重置编码器的数值为0
参数:无
返回值:无
功能:设置编码器的数值为多少
参数:编码器值,单位
返回值:无
例子程序
robot-config.h
vex::brain Brain;
vex::controller Controller = vex::controller();
vex::encoder Encoder = vex::encoder(Brain.ThreeWirePort.A); //配置编码端口
main.cpp
#include "robot-config.h"
int main() {
while(1){
if(Controller.ButtonL1.pressing()){ //遥控器L1按键是否按下,如果是
Encoder.resetRotation(); //编码器清零
// Encoder.setRotation(0, vex::rotationUnits::deg); //编码器清零
Brain.Screen.printAt(1, 40, "Successfully resetRotation!");
vex::task::sleep(1000);
}
vex::task::sleep(100);
}
}
传感类
功能:读取编码器的数值
参数:单位
返回值:double
例子
robot-config.h
//定义四个电机和编码器
vex::brain Brain;
vex::controller Controller = vex::controller();
vex::encoder Encoder = vex::encoder(Brain.ThreeWirePort.A);
vex::motor L1 = vex::motor(vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor L2 = vex::motor(vex::PORT2, vex::gearSetting::ratio18_1, false);
vex::motor R1 = vex::motor(vex::PORT10, vex::gearSetting::ratio18_1, true);
vex::motor R2 = vex::motor(vex::PORT9, vex::gearSetting::ratio18_1, true);
main.cpp
#include "robot-config.h"
void go_straight(int speed, int distance){
Encoder.resetRotation();
L1.spin(vex::directionType::fwd, speed, vex::velocityUnits::pct);
L2.spin(vex::directionType::fwd, speed, vex::velocityUnits::pct);
R1.spin(vex::directionType::fwd, speed, vex::velocityUnits::pct);
R2.spin(vex::directionType::fwd, speed, vex::velocityUnits::pct);
while(Encoder.rotation(vex::rotationUnits::deg) < distance ){
vex::task::sleep(100);
}
L1.stop(vex::brakeType::brake);
L2.stop(vex::brakeType::brake);
R1.stop(vex::brakeType::brake);
R2.stop(vex::brakeType::brake);
}
int main() {
go_straight(70, 1000);
}
功能:读取编码器的速度
参数:速度单位
返回值:double
例子
未完待续…