|
悬赏1乐币已解决
//remote controlled robot
// I/O devices ports
#define R_WHEEL OUT_B
#define L_WHEEL OUT_C
#define WHEELS OUT_BC
#define TOOL OUT_A
// position of the graphics on screen
#define L_INFO_PX 25
#define R_INFO_PX 75
// possible speed values
#define SPEED1 (20)
#define SPEED2 (50)
#define SPEED3 (70)
#define SPEED4 (100)
// commands constant definitions
#define STOP 0
#define FWD1 1
#define FWD2 2
#define FWD3 3
#define FWD4 4
#define REV1 5
#define REV2 6
#define REV3 7
#define REV4 8
// change this constant
// to get the robot going forward
// when getting a FWD command
// (allowed values are 1 or -1)
#define DIRECTION -1
// this function returns true if the
// Bluetooth connection with robot exists,
// otherwise it displays some instructions
// and returns false
bool CheckBTConnection(int conn)
{
bool connection_exists;
if (BluetoothStatus(conn)==NO_ERR)
{
connection_exists = true;
}
else
{
ClearScreen();
TextOut(0,LCD_LINE1,"You must connect");
TextOut(0,LCD_LINE2,"this NXT on");
TextOut(0,LCD_LINE3,"BT channel 1");
TextOut(0,LCD_LINE4,"using the menu");
TextOut(0,LCD_LINE5,"of the remote");
TextOut(0,LCD_LINE6,"control NXT.");
Wait(4000);
connection_exists = false;
}
return connection_exists;
}
// this function convert the received command
// opcode into the corresponding motor speed
short DecodeSpeed(short cmd)
{
short speed;
if ( cmd == STOP ) speed = 0;
if ( cmd == FWD1 ) speed = SPEED1;
if ( cmd == FWD2 ) speed = SPEED2;
if ( cmd == FWD3 ) speed = SPEED3;
if ( cmd == FWD4 ) speed = SPEED4;
if ( cmd == REV1 ) speed = -SPEED1;
if ( cmd == REV2 ) speed = -SPEED2;
if ( cmd == REV3 ) speed = -SPEED3;
if ( cmd == REV4 ) speed = -SPEED4;
return speed*sign(DIRECTION);
}
// this subroutine actuates the command received:
// if continuous is true, the TOOL motor is run
// while the remote buttons are pressed,
// if continuous is false, the TOOL motor is run stepping
// only if function is different from old,
// that holds the precedent value of function
sub Move(byte Lcmd, byte Rcmd, byte function, byte old, bool continuous)
{
// float wheels if both Xc are equal to STOP
if (Rcmd == STOP) Float(R_WHEEL);
if (Lcmd == STOP) Float(L_WHEEL);
if (Rcmd == Lcmd)
{
// if the speed of the wheels is the same,
// run motors in sync
OnFwdSync(WHEELS,DecodeSpeed(Rcmd),0);
}
else
{
// else run motor at the speed
// given by the DecodeSpeed function
OnFwd(R_WHEEL,DecodeSpeed(Rcmd));
OnFwd(L_WHEEL,DecodeSpeed(Lcmd));
}
// if the commands can be continuous
if (continuous)
{
switch (function)
{
case 0:
Off(TOOL);
break;
case 1:
OnFwd(TOOL,50);
break;
case 2:
OnFwd(TOOL,100);
break;
case 3:
OnRev(TOOL,100);
break;
}
}
// if the commands are one-shot
else if (function!=old)
{
switch (function)
{
case 1:
PlayTone(1000,10);
RotateMotorPID(TOOL,100,180,50,30,70);
break;
case 2:
PlayTone(2000,10);
RotateMotorPID(TOOL,100,45,50,30,70);
break;
case 3:
PlayFile("! Attention.rso");
RotateMotorPID(TOOL,100,-360,50,30,70);
break;
}
}
}
// this subroutine displays informations
// about the command received
sub ShowCommands(byte Lc, byte Rc, byte trig, short command)
{
string arrows[];
string Rstring, Lstring;
// fill the arrows array with the listed strings
ArrayBuild(arrows,"o",">",">>",">>>",">>>>","<","<<","<<<","<<<<");
ClearScreen();
// prepare the Xstring to be shown,
// according to the right and left command values.
// The Xstring is an element of the arrows array.
Rstring = arrows[Rc];
Lstring = arrows[Lc];
// show the string on the screen.
TextOut(R_INFO_PX-5*StrLen(Rstring),LCD_LINE2,Rstring);
TextOut(L_INFO_PX-5*StrLen(Lstring),LCD_LINE2,Lstring);
if (trig&1) TextOut(R_INFO_PX,LCD_LINE3,"*");
if (trig&2) TextOut(L_INFO_PX,LCD_LINE3,"*");
NumOut(40,LCD_LINE6,command);
}
task main ()
{
short command;
byte Rcmd, Lcmd, function, oldfunction;
if (!CheckBTConnection(0))
{
// if BT connection with remote control
// is not established, stop the whole program
Stop(true);
}
// loop forever
while(true)
{
// if the mailbox 1 is not empty, process command
if (ReceiveRemoteNumber(1,true,command) != STAT_MSG_EMPTY_MAILBOX)
{
// decode the left command (100's)
Lcmd = (command/100);
// decode the right command (10's)
Rcmd = (command/10)%10;
// decode the function (1's)
function = (command%10);
// show various informations on screen
ShowCommands(Lcmd,Rcmd,function,command);
// actuate command
Move(Lcmd,Rcmd,function,oldfunction,false);
oldfunction = function;
}
Wait(50);
}
}
|
最佳答案
查看完整内容
/ /远程控制的机器人
/ / I / O设备端口
#定义R_WHEEL OUT_B
#定义L_WHEEL OUT_C
#定义轮OUT_BC
#定义工具OUT_A
在屏幕上的图形/ /位置
#定义L_INFO_PX 25
#定义R_INFO_PX 75
/ /可能的速度值
#定义SPEED1 ( 20 )
#定义SPEED2 ( 50 )
#定义SPEED3 ( 70 )
#定义SPEED4 ( 100 )
/ /命令常量定义
#定义STOP 0
#定义FWD1 1
#定义FWD2 2
#定义FWD3 3
#定义FWD4 4
#定义REV1 5
#定义REV2 6
#定义REV3 7
# ...
|