练习简单地通过switch语句实现状态迁移.
小车自动往返于接料位置和投料位置.
在自动状态下:
触碰到接料位行程开关后停止移动, 开始计时, 接料时间过后往投料位移动.
触碰到投料位行程开关后停止移动, 开始计时, 投料时间过后往接料位移动.
在手动状态下:
可以通过手动按钮自由移动小车位置.
– [F]:切换手自动状态
– [A/D]:手动状态下控制小车前进/后退
– [Q]:退出程序
//- --- --- ---
//- --- --- ---
//--
ZcTrolley pbTheTrolley=new ZcTrolley();
//--
void setup(){
size(320,240);frameRate(16);
}//+++
void draw(){
background(0);
pbTheTrolley.mnPositiveSW=(keyPressed&&key=='d');
pbTheTrolley.mnNegativeSW=(keyPressed&&key=='a');
pbTheTrolley.ccUpdate();
drRailAndTrolley(pbTheTrolley.dcPos/100);
//--
fill(0x77,0xEE,0x77);
//--
if(pbTheTrolley.dcNLimit){text("charge:" +nf(pbTheTrolley.cmStageInterval,2), 99, 80);}
if(pbTheTrolley.dcPLimit){text("discharge:"+nf(pbTheTrolley.cmStageInterval,2),180,136);}
//--
fnTextBool("shuttle:",pbTheTrolley.mnRunSW,2);
fnTextFloat("position:",pbTheTrolley.dcPos,3);
fnTextInt("stage:",pbTheTrolley.cmStage,4);
fnTextBool("PMC:",pbTheTrolley.dcPositiveMC,5);
fnTextBool("NMC:",pbTheTrolley.dcNegativeMC,6);
fnTextBool("PLS:",pbTheTrolley.dcPLimit,7);
fnTextBool("NLS:",pbTheTrolley.dcNLimit,8);
//--
text("[F]to toggle shuttle..\n[A/D]to mannually move trolley\n while not shuttling..\n[Q]to quit..",5,188);
//--
}//+++
void keyPressed(){switch(key){
case 'f':pbTheTrolley.ccFlipRunSwitch();break;
case 'q':exit();break;
default:break;
}}//+++
//- --- --- ---
//- --- --- ---
void fnTextFloat(String cmLable, float cmVal, int pxLine){
text(cmLable+nfc(cmVal,2),16,pxLine*16);
}//+++
void fnTextInt(String cmLable, int cmVal, int pxLine){
text(cmLable+nf(cmVal,4),16,pxLine*16);
}//+++
void fnTextBool(String cmLable, boolean cmVal, int pxLine){
text(cmLable+nf(cmVal?1:0,1),16,pxLine*16);
}//+++
//- --- --- ---
//- --- --- ---
void drRailAndTrolley(float pxPercent){
fill(0x77);rect(99,99,128,8);
float lpX=110+pxPercent*78;
float lpY=95;
fill(0xEE);quad(
lpX,lpY,
lpX+26,lpY,
lpX+20,lpY+26,
lpX+6,lpY+26
);
}//+++
//- --- --- ---
//- --- --- ---
class ZcTrolley{
//--
static final int C_TIMER_DEFAULT=4;
static final int C_TIMER_STARTWAIT=16;
static final int C_TIMER_CHARGEWAIT=32;
static final int C_TIMER_DISCHARGEWAIT=32;
//--
boolean mnRunSW=false;
boolean mnPositiveSW=false;
boolean mnNegativeSW=false;
//--
boolean cmPositiveFG=false;
boolean cmNegativeFG=false;
int cmStage=0;
int cmStageInterval=0;
//--
float dcPos=50;
boolean dcNLimit=false;
boolean dcPLimit=false;
boolean dcPositiveMC=false;
boolean dcNegativeMC=false;
//--
ZcTrolley(){;}
void ccUpdate(){ccScan();ccSimulate();}
//--
void ccScan(){
//--
cmStageInterval--;if(cmStageInterval<0){cmStageInterval=0;}
boolean lpStageTimePulse=(cmStageInterval==1);
boolean lpStageTimeUp =(cmStageInterval==0);
//--
switch(cmStage){
case 0:
cmPositiveFG=false;
cmNegativeFG=false;
if(mnRunSW){cmStage=1;cmStageInterval=C_TIMER_STARTWAIT;}
break;
//--
case 1:if(!lpStageTimeUp){break;}
cmNegativeFG=true;
if(dcNLimit){
cmNegativeFG=false;
cmStage=2;cmStageInterval=C_TIMER_DEFAULT;
}
break;
//--
case 2:if(!lpStageTimeUp){break;}
/*NOP*/
if(true){cmStage=3;cmStageInterval=C_TIMER_CHARGEWAIT;}
break;
//--
case 3:if(!lpStageTimeUp){break;}
cmPositiveFG=true;
if(dcPLimit){
cmPositiveFG=false;
cmStage=4;cmStageInterval=C_TIMER_DEFAULT;
}
break;
//--
case 4:if(!lpStageTimeUp){break;}
/*NOP*/
if(true){cmStage=1;cmStageInterval=C_TIMER_DISCHARGEWAIT;}
break;
//--
default:break;
}if(!mnRunSW){cmStage=0;}
//--
dcPositiveMC=(cmPositiveFG&&mnRunSW)||(mnPositiveSW&&!mnRunSW);
dcNegativeMC=(cmNegativeFG&&mnRunSW)||(mnNegativeSW&&!mnRunSW);
//--
}
//--
void ccSimulate(){
if(dcPositiveMC){dcPos+=0.4;}
if(dcNegativeMC){dcPos-=0.4;}
dcPos=constrain(dcPos,1,99);
dcNLimit=(dcPos<2 );
dcPLimit=(dcPos>98);
}
//--
void ccFlipRunSwitch(){mnRunSW=!mnRunSW;}
//--
}//+++
- 本文固定链接: http://iprocessing.cn/2017/10/13/习作bc_模拟自动往返投料小车/
- 转载请注明: constrain 于 Processing编程艺术 发表