float batteryDrain = getBatteryCurrent();当前电池用电量吧
float batteryLife = getBatteryVoltage();还有多少电池水平。
float timeUntilDown = getPowerDownTimer();自动关机时间。
float pd=getPowerDownTimer();
displayBigTextLine(5,"powerdown=%0.1f",pd);
float batt=getBatteryVoltage();
float bat1=getBatteryCurrent():
displayBigTextLine(3,"%0.1f%0.1f",batt,bat1);显示数字。
setPowerDownTimer(5);设置自动关机时间。设好后下次一直就是这个时间了。
motor(motorA)=50;A口电机以功率50运行。
displayBigTextLine(3,"abc");第三行大字体靠左显示abc;
displayCenteredBigTextLine(2,"rrob");第二行大字体居中显示rrob
displayCenteredTextLine(3,"ROBOTC");第三行剧中显示标准字体
The x value is above 177.
The y value is above 127.
displayBigStringAt(0, 16, "A");以 x y 座标显示。 从左下角为0,0;
clearPixel(50, 32);清除像素点信息。
for(inti=1;i<50;i++)
{
for(intj=1;j<50;j++)
{
clearPixel(i,j);
}
}
displayInverseBigStringAt(10, 20,"ROBOTC"); 像素点10,20黑底白字显示robotc.
displayString(3, "ROBOTC"); 第三行显示robotc
displayTextLine(3, "ROBOTC");第三行显示robotc
drawLine(10,10,167,10);
drawLine(10,10,10,117);
drawLine(10,117,167,117); 画个正方形。
drawLine(167,10,167,117);
drawRect(20, 107, 157, 20); 左 上 右 下。画方形
drawEllipse(10, 40, 100, 15);左 上 右 下。画圆形
eraseDisplay();清屏;
drawLine(10,10,167,117);
drawLine(10,117,167,10);画线。以坐标。
eraseLine(10,10,167,117); 擦除画的线。
eraseRect(20,107,157,20);把左上右下,内的信息都删了。
fillEllipse(30, 97, 147, 30);画实心圆。
fillRect(55, 97, 122, 30);画实心正方形。
invertPixel(5,10); 填实像素。
setPixel(x, y);画点。
setLEDColor(ledRed); 指示灯变红色。
setLEDColor(ledOrangeFlash)橙色一直闪。
setLEDColor(ledGreenPulse)绿色闪二下。节凑。
sleep(5000);
buttonNone: No button (0)
buttonUp: Up button (1)
buttonEnter: Enter button (2)
buttonDown: Down button (3)
buttonRight: Right button (4)
buttonLeft: Left button (5)
buttonBack: Back button (6)
buttonAny: Any button (7)
if(getButtonPress(LEFT_BUTTON)) 程序块上左边按键按下。是1 没有按是0;
{
setMotorSpeed(motorA,50); 设置A电机的速度50
}else
{
setMotorSpeed(motorA,-50); 设置A电机的速度为-50
}
getTouchValue(S1)==0 取S1 按压传管器。
setBlockBackButton(1); 程序块上的返回值 不会退出程序 。。0 可以退出。
waitForButtonPress(); 等街程序块上的任意按健,程序继续运行下去。
bFloatDuringInactiveMotorPWM = true; // the motors WILL coast when power is notapplied
bFloatDuringInactiveMotorPWM = false; // the motors will NOT coast when power isnot applied
getMotorBrakeMode(motorA)!= motorCoast 返回A电机不是滑行方式
getMotorBrakeMode(motorA)!= motorBrake 返回A电机不是制动方式
setMotorBrakeMode(motorA,motorCoast);设置A电机滑行方式,
setMotorBrakeMode(motorA,motorBrake);设置A电机制动方式。
setMotorSpeed(motorA,100);
sleep(5000);
setMotorSpeed(motorA,0);
sleep(50000);
重置A电机的度数为0
resetMotorEncoder(motorA);
设置a电机以50的功率,转1000度。
setMotorTarget(motorA, 1000, 50);
一直等电机转到设置的数度就结束。repeatUntil 是条件满足退出 while 是条件不满足退出。
repeatUntil(getMotorEncoder(motorA) ==getMotorTarget(motorA)) 取A电机转的角度,
{
}
resetMotorEncoder(motorA);
//resetMotorEncoder(motorD);
setMotorTarget(motorA,1000,50);
//setMotorTarget(motorD,-100,50);
while(getMotorTarget(motorA)!=getMotorEncoder(motorA)) ////repeatUntil(getMotorTarget(motorA)==getMotorEncoder(motorA))
{
eraseDisplay();
displayBigTextLine(2,"targe=%d",getMotorTarget(motorA));
displayBigTextLine(4,"encode=%d",getMotorEncoder(motorA));
sleep(50);
}
if(getMotorRPM(motorA)< 100) 返回A电机的每分钟运行多少圈 。
{
//Increasethe value of speed
speed= speed + 1;
//Briefsleep period (2 milliseconds)
sleep(2);
}
//Thenapply the value of speed to the motors
//Thisshould cause the speed to 'ramp' up
setMotorSpeed(motorA,speed);
setMotorSpeed(motorD,speed);
displayBigTextLine(2,"%d",getMotorSpeed(motorA));返回电机功率。
displayBigTextLine(4,"%d",getMotorSpeed(motorD));
displayBigTextLine(6,"%d",getMotorRPM(motorA));
}
moveMotorTarget(motorA, 1000, 50);
//Keep looping until the motor stops moving
repeatUntil(getMotorMoving(motorA) ==0); 电机转到一定的角度就停止。
{
//Donothing (idle loop)
}
getMotorMoving(motorA) 电机是否在移动。
setMotorSpeed(motorA,50); getMotorSpeed(motorA);
moveMotorTarget(MotorA,1000,50); getMotorMoving(motorA)==0
getMotorRunning(motorA) == 0 电机是否在运行。
int speedA=getMotorSpeed(motorA); 整形。A电机功率。
moveMotorTarget(motorA,1000,100); 以100的功率 向前转1000度。
waitUntilMotorStop(motorA);等待A电机停止。////好。
moveMotorTarget(motorA,1000,100);
waitUntilMotorStop(motorA);加了等电机a电机转好后在在继续向下运行。不加两个电机一起转。
moveMotorTarget(motorD,-1000,50);
waitUntilMotorStop(motorD);
//Setsthe Left Motor to the left side of the drive train
//Therobot should move forward for 1 rotation as expected
nMotorDriveSide[leftMotor]= driveLeft;
forward(1,rotation, 50);
//Setsthe Left Motor to the right side of the drive train
//Thiswill cause undesired behavior from the forward command
nMotorDriveSide[leftMotor]= driveRight;
forward(1,rotation, 50);
resetMotorEncoder(motorA);重置A电机角度为0。
setMotorBrakeMode(motorA, motorCoast);
//Set a motor target of 360 encoder counts
setMotorTarget(motorA, 360, 0);
//Wait until the motor has reached itstarget and stops
waitUntilMotorStop(motorA);
//Tell the program to sleep for 1000ms (1second)
//Check the encoder value in the debugger
sleep(1000);
//Resets the motor encoder to 0
resetMotorEncoder(motorA);
//The motor will now Brake(resistsmovement)
setMotorBrakeMode(motorA, motorBrake);
//Set a motor target of 360 encoder counts
setMotorTarget(motorA, 360, 0);
//Wait until the motor has reached itstarget and stops
waitUntilMotorStop(motorA);
//Tell the program to sleep for 1000ms (1second)
//Check the encoder value in the debugger
sleep(1000);
//Set motor to be reversed
setMotorReversed(motorA, true); 设置A电机反转为真。
//The motor runs counter-clockwise withpositive speeds (reversed)
setMotorSpeed(motorA, 50);
//Set motor to not be reversed
setMotorReversed(motorA, false); 设置电机反转为假。
//The motor runs clockwise with positivespeeds (normal)
setMotorSpeed(motorA, 50);
//Sets the speed value of motor motor portA to full forward (+100)
setMotorSpeed(motorA, 100); 设置电机功率100 的速度。
//Halt program flow for 1000ms (1 second)
sleep(1000);
//Sets the speed value of motor motor port Ato half-speed backwards (-50)
setMotorSpeed(motorA, -50);倒着电机速度。
//Halt program flow for 1000ms (1 second)
sleep(1000);
//Sets the speed value of motor motor port Ato stopped (0)
setMotorSpeed(motorA, 0);停止电机
//Halt program flow for 1000ms (1 second)
sleep(1000);
//Synchs left and right motor at 50 turnratio
//at power level 60 for 1 second
setMotorSync(leftMotor, rightMotor, 0, 60);
sleep(1000);
//Synchs left and right motor at 50 turnratio
//at power level 60 for 1 second
setMotorSync(leftMotor, rightMotor, 50,60);
sleep(1000);
//Synchs left and right motor at 50 turnratio
//at power level 60 for 1 second
setMotorSync(leftMotor, rightMotor, 100,60);
sleep(1000);
#pragma config(Sensor, S1, touchSensor, sensorEV3_Touch)
#pragma config(Sensor, S2, gyroSensor, sensorEV3_Gyro, modeEV3Gyro_RateAndAngle)
#pragma config(Sensor, S3, colorSensor, sensorEV3_Color, modeEV3Color_Color)
#pragma config(Sensor, S4, sonarSensor, sensorEV3_Ultrasonic)
#pragma config(Motor, motorA, armMotor,tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorB, leftMotor,tmotorEV3_Large, PIDControl, driveLeft, encoder)
#pragma config(Motor, motorC, rightMotor,tmotorEV3_Large, PIDControl, driveRight, encoder)
//*!!Code automatically generated by'ROBOTC' configuration wizard !!*//
该 pragma 伪指令具有以下形式
task main()
{
//Sets the LeftMotor to the left side of the drive train
//Therobot should move forward for 1 rotation as expected
nMotorDriveSide[leftMotor]= driveLeft;
//Setsthe Left Motor to the right side of the drive train
//Thiswill cause undesired behavior from the forward command
nMotorDriveSide[leftMotor]= driveRight;
setMotorSync(leftMotor,rightMotor,0,60);双电机同步。 0直走。100掉头。
sleep(1000);
setMotorSync(leftMotor,rightMotor,50,60); 左电机 右电机 电机转向。功率。
sleep(1000);
}
setMotorSyncEncoder(leftMotor,rightMotor,0,360,50);双电机直行同步运行360度。以功率50
waitUntilMotorStop(leftMotor);一定要等电机停止后才可以运行下一步。
setMotorSyncEncoder(leftMotor,rightMotor,100,360,50);双电机掉头。360度,电机50功率
setMotorSyncEncoder(motorB,motorC,0,360,50); 同步电机也可以motorA motorB.
waitUntilMotorStop(leftMotor);等待电机停止后可以运行。
setMotorSyncEncoder(motorB,motorC,100,360,50);
waitUntilMotorStop(rightMotor);
setMotorSync(leftMotor,rightMotor,0,50);
sleep(2000);
setMotorSyncTime(motorB,motorC,0,1000,50); 同步电机BC 直行,1秒,功率50
waitUntilMotorStop(rightMotor);等待电机停止。
setMotorSyncTime(motorB,motorC,100,1000,50);同步电机BC掉头。1秒。功率50。
//Sets the motorA's target to 1000 encodercounts at a speed value of 50
setMotorTarget(motorA, 1000, 50);设置A电机转1000度功率50
moveMotorTarget(motorA,1000, 50);
//Holds program flow until the motor on motorport A comes to a complete stop.
waitUntilMotorStop(motorA);等电机停止后在继续运行。
Sensor
displayCenteredBigTextLine(3,"%d",getColorAmbient(S3)); 环境光。0到100。
displayCenteredBigTextLine(3,"%d",getColorReflected(S3)); 反射光。Short类型。
displayCenteredBigTextLine(3,"%d",getColorHue(S3));返回0。360的颜色。
getColorRGB(S3,r,g,b); 返回RGB三色。
displayCenteredBigTextLine(2,"R=%d",r);
displayCenteredBigTextLine(4,"G=%d",g);
displayCenteredBigTextLine(6,"B=%d",b);
displayCenteredBigTextLine(8,"Hue=%d",getColorHue(S3));
sleep(50);
int threshold = 50;
while(true)
{
//Ifthe color sensor senses the line (a dark value
//lowerthan the calculated threshold of 50):
if(getColorReflected(S3) < threshold)
{
setMotorSpeed(motorC, 50);
setMotorSpeed(motorB,0);
}
else
{
setMotorSpeed(motorC,0);
setMotorSpeed(motorB,50);
}
}
intco=0;
while(1)
{
co=getColorName(S3); 返回色彩名,0 到7 个色彩。,
displayCenteredBigTextLine(2,"%d",co);
switch(co)
{
case 0:
displayCenteredBigTextLine(4,"colorNone");
break;
case 1:
displayCenteredBigTextLine(4,"colorBlack");
break;
case 2:
displayCenteredBigTextLine(4,"colorBlue");
break;
case 3:
displayCenteredBigTextLine(4,"colorGreen");
break;
case 4:
displayCenteredBigTextLine(4,"colorYellow");
break;
case 5:
displayCenteredBigTextLine(4,"colorRed");
break;
case 6:
displayCenteredBigTextLine(4,"colorWhite");
break;
case 7:
displayCenteredBigTextLine(4,"colorBrown");
break;
default:
displayCenteredBigTextLine(4,"color no");
}
sleep(50);
}
getColorSaturation(S3) 返回色采饱和度。0 到100;
dis=getUSDistance(S4); 返回超升波的距离
repeatUntil(0) constant '0'. Loop will never exit.
resetGyro(S2); 重置陀螺仪,
repeatUntil(getGyroDegrees(S2) > 90) 陀螺仪反回角度。
{
setMotorSpeed(motorC,-50);
setMotorSpeed(motorB,50);
}
//Stop the motors at the end of the turn
setMotorSpeed(motorB, 0);
setMotorSpeed(motorC, 0);
displayCenteredBigTextLine(4,"%d",getGyroDegrees(S2)); 返回 角度
displayCenteredBigTextLine(6,"%d",getGyroHeading(S2));返回增加减少了多少角度。
getGyroRate(S2)返回转动的速度,移动的速度?
getIRBeaconDirection(S4) 信标方向
if(getIRBeaconDirection(S4) > 5)
{
//TurnRight
setMotorSpeed(leftMotor,50);
setMotorSpeed(rightMotor,-50);
}
//Ifthe IR sensor (port 4) sees a beacon to the left (negative)
elseif(getIRBeaconDirection(S4) < -5)
{
//TurnLeft
setMotorSpeed(leftMotor,-50);
setMotorSpeed(rightMotor,50);
}
//Otherwise,move forward
else
{
//MoveForward
setMotorSpeed(leftMotor,50);
setMotorSpeed(rightMotor,50);
}
}//跟着信标走。
//While the IR beacon's signal strength isless than 20
while(getIRBeaconStrength(S4) < 20)
{
//Spinin place (seek the beacon)
setMotorSpeed(leftMotor,50);
setMotorSpeed(rightMotor,-50);
}//跟着IR信标走。
while(getIRDistance(S4) > 45) 测试红外距离。
{
//Keepmoving forward
setMotorSpeed(leftMotor,50);
setMotorSpeed(rightMotor,50);
}
//Stop the robot
setMotorSpeed(leftMotor, 0);
setMotorSpeed(rightMotor, 0);
getIRRemoteButtons(S4)
getIRRemoteChannelButtons(S4, 1)
resetBumpedValue(S1); 重置触动传感器。
repeatUntil(getBumpedValue(S1)==15) 触动传感器。按一下加一。
{
displayCenteredBigTextLine(2,"%d",getBumpedValue(S1));
resetBumpedValue(S1);
repeatUntil(getBumpedValue(S1)==15)
{
displayCenteredBigTextLine(2,"%d",getBumpedValue(S1));
displayCenteredBigTextLine(4,"%d",getTouchValue(S1));按下为1 没有按为0
}
,SensorRaw(S4)返回原始传感器的数值。
SensorType(S1) = sensorEV3_Touch; 设置传感器类型。
while(true) //Infinite loop:
{
//display "Sensor Value: ##"
displayCenteredBigTextLine(3,"SensorValue: %d",sensorValue(S1));
wait1Msec(100);
eraseDisplay();
}
Sound
//Clear the sound buffer
clearSounds(); 停止声音
•Values range from 0 to 100 (percentages)
setSystemVolume(75);声音大小
speakerVolume = getVolume(); 得到声音大小。
PlayImmediateTone(440, 50);
wait1Msec(200);
playSound(soundBeepBeep);
displayCenteredBigTextLine(2,"%d",getSoundVolume());
playSound(soundBeepBeep);
sleep(1000);
//playSound(soundBlip);
sleep(1000);
playSound(soundDownwardTones);
sleep(1000);
//playSound(soundException);
sleep(1000);
//playSound(soundFastUpwardTones);
sleep(1000);
playSound(soundLowBuzz);
sleep(1000);
playSound(soundShortBlip);
sleep(1000);
//playSound(soundUpwardTones);
sleep(5000);
◦C:\Program Files (x86)\RobomatterInc\ROBOTC Development Environment 4.X\EV3 System Files\Sounds
playSoundFile("Bravo");
playTone(784, 15);
task control
// Aborts the current time slice of the task
abortTimeslice();
// Suspend every other task and focus 100%of the CPU on the current task
hogCPU();
int myPriority = 100;
//Sets 'mypriority' to 7 (default) 默认7 最高255。任务优先级。
myPriority = kDefaultTaskPriority;
myPriority =kHighPriority;最高255
myPriority =kLowPriority;最低为0;
// Assigns the current task with a priorityof 100
nSchedulePriority = 100;
// Resume other tasks and return control tothe task scheduler
•It cancelsthe effect of a previous HogCPU() function call.
releaseCPU();
// Starts the task named"MoveArm" with a priorty of 10
startTask(MoveArm, 10);任务名,优先级10l
// Stops all tasks currently running andexits the current program
stopAllTasks();
// Stops the task named "MoveArm"
stopTask(MoveArm);
int threshold = 600;
clearTimer(T1); // Resets timer T1 to startcounting at 0
while(time1[T1] < 10000) // Loops code while timer T1 has counted lessthan 10 seconds
{
if(getColorValue(lineDetector) <threshold)
{
// counter-steer right:
setMotorSpeed(leftMotor, 50); //Set the leftMotor (motor1) tohalf power (50)
setMotorSpeed(rightMotor, 0); //Set the rightMotor (motor6) to off (0)
}
else
{
//counter-steer left:
setMotorSpeed(leftMotor, 0); //Set the leftMotor (motor1) tooff (0)
setMotorSpeed(rightMotor, 50); //Set the rightMotor (motor6) to half power(50)
}
}
}
delay(1000);
// Stores the current value of nPgmTime toa variable
varPgmTime = nPgmTime;
// Stores the current value of nSysTime toa variable
varSysTime = nSysTime;
sleep(1000);
//Sets the robot to drive forward bysetting ports A, B, C, and D at half speed for 1 second
setMultipleMotors(50, motorB, motorC);
wait(1);
stopMultipleMotors(motorB, motorC);
//Stops all of the motors attached to theEV3
stopAllMotors();
//To set motor A to speed -50 for 0.5seconds:
setMotor(motorA, -50);
wait(0.5);
stopMotor(port8);//stops motor
stopMultipleMotors(rightMotor, leftMotor,armMotor);
resetTimer(T1);
while(getTimer(T1,milliseconds)<2000){
}
stopAllmotors();c