vex edr v5之轴角编码器和超声波传感器


硬件: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

例子

未完待续…

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值