旋钮编码器c代码_vex如何用robotc编写利用编码器控制电机转动的程序?

这段C代码展示了如何利用VEX RobotC编程,通过旋钮编码器来控制机器人电机转动。程序中配置了两个编码器(右电机和左电机)并设定初始值为0。当右编码器计数小于1800时,机器人左转;当左编码器计数小于1800时,机器人右转。整个过程电机以全速运行。
摘要由CSDN通过智能技术生成

#pragma config(Sensor, dgtl1, rightEncoder, sensorQuadEncoder)

#pragma config(Sensor, dgtl3, leftEncoder, sensorQuadEncoder)

#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed)

#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()

{

wait1Msec(2000); //Robot waits for 2000 milliseconds before executing program

SensorValue[rightEncoder] = 0; //Set the encoder so that it starts counting at 0

while(SensorValue(rightEncoder) < 1800) //While rightEncoder has counted less than 1800 counts

{

//Turn Left

motor[rightMotor] = 127; //Motor on port2 is run at full (127) power forward

motor[leftMotor] = -127; //Motor on port3 is run at full (-127) power reverse

}

SensorValue[leftEncoder] = 0; //Set the encoder so that it starts counting at 0

while(SensorValue(leftEncoder) < 1800) //While leftEncoder has counted less than 1800 counts

{

//Turn Right

motor[rightMotor] = -127; //Motor on port2 is run at full (-127) power reverse

motor[leftMotor] = 127; //Motor on port3 is run at full (127) power forward

}

}

具体情况具体对待吧 其实ROBOTC软件里有编程范例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值