硬件: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
例子
未完待續…