2021年山东物联网技能大赛Android组第一场比赛
1、物料监控功能
请选手新建 Android 项目根据任务要求完成相应功能开发。
任务要求:
要求开发物料监控系统,监控搅拌机的运行情况,并根据实际情况及时
补充物料,程序界面效果图如下。
1 程序通过 NEWPorter 监测搅拌机的速度(使用直流电机转速判断)。在
程序界面需要实时显示当前转速,在程序界面使用动画来显示低速、正
常速、高速的搅拌效果。
2 当转速太快时,位于程序右下角区域显示转速太快,同时工位上报警灯
亮起。当转速恢复正常时,报警灯熄灭同时界面上不显示“转速太快相
关提示”页面效果如下图所示
3 当转速太慢时,需要自动开始补充物料(使用电动推杆伸出过来代替),
在物料补充的过程中工位上三色灯黄灯亮起。物料补充完成(电动推杆
完全伸出状态代替)后 3 秒电动推杆自动缩回,此时三色灯黄灯保持亮
起,当推杆完全缩回,三色灯黄灯熄灭同时三色灯绿灯亮起。
4 在物料补充过程中任意时间触发工位上微动开关(不松开),可以暂停
物料补充,工位上三色灯红灯亮起。松开微动开关,继续完成物料补充
过程同时三色灯红灯熄灭。
5 开发完成后将程序以“物料监控”命名发布到物联网应用开发终端。
首先onCreate()函数里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
kaishi();
start2();
start_change();
}
Init();初始化函数
private void Init() {
getSupportActionBar().hide();
(tv_val) =findViewById(R.id.tv_val);
(iv_fan) =findViewById(R.id.iv_fan);
(ll_1) =findViewById(R.id.ll_1);
df = new DecimalFormat("0.00");
high = AnimationUtils.loadAnimation(this,R.anim.high);
low = AnimationUtils.loadAnimation(this,R.anim.low);
normal = AnimationUtils.loadAnimation(this,R.anim.normal);
modbus4150 = new Modbus4150(DataBusFactory.newSocketDataBus("172.20.6.15", 6003), new ConnectResultListener() {
@Override
public void onConnectResult(boolean b) {
}
});
}
回调函数
public void getTime(final callBack callback){
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (timerTaskBool){
try {
flag = getVal(1);
callback.getflag(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
timer = new Timer();
timer.schedule(timerTask,0,2000);
}
kaishi()
private void kaishi() {
new Thread(new Runnable() {
@Override
public void run() {
while (true){
if (isD1){
if (getVal(6) == 1){ //判断微动
SystemClock.sleep(200);
if (getVal(6) == 1 && !isCome){
isCome = true;
control(2,false); //电动 停
control(1,false);
control(5,true); //红灯 亮
}
}else if (getVal(6) == 0 && isCome) {
isCome = false;
control(1, true); //电动 开
control(2, false);
control(5, false); //红灯
}
if (getVal(4) == 1){
isD1 = false;
isClose = true;
control(2,true); //电动 关
control(1,false);
}
}
}
}
}).start();
}
start_change();
private void start_change() {
getTime(new callBack() {
@Override
public void getflag( int flag) {
if (flag ==1 && !isChange){
startTime = System.currentTimeMillis();
isChange = true;
}
if (flag == 0 && isChange){
endTime = System.currentTimeMillis();
isChange = false;
done = (endTime -startTime)*2;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
val = Double.valueOf(1.0/(done/1000.0));
tv_val.setText(df.format(val)+"r/s");
if ( val>2.4){
ll_1.setVisibility(View.VISIBLE);
if (iv_fan.getAnimation() != high){
iv_fan.startAnimation(high);
new Thread(new Runnable() {
@Override
public void run() {
control(0,true);
}
}).start();
}
}else if ( val<0.3){
if (ll_1.getVisibility() == View.VISIBLE){
ll_1.setVisibility(View.GONE);
}
if (iv_fan.getAnimation() != low){
iv_fan.startAnimation(low);
new Thread(new Runnable() {
@Override
public void run() {
control(0,false);
}
}).start();
}
if (!isStart){
isStart = true;
timerTaskBool = false;
new Thread(new Runnable() {
@Override
public void run() {
control(3,true); //黄灯 开
control(4,false);
control(5,false);
control(1,true); //电动 开
control(2,false);
}
}).start();
isD1 = true;
}
}else{
if (ll_1.getVisibility() == View.VISIBLE){
ll_1.setVisibility(View.GONE);
}
if (iv_fan.getAnimation() != normal){
iv_fan.startAnimation(normal);
new Thread(new Runnable() {
@Override
public void run() {
control(0,false);
}
}).start();
}
}
}
});
}
});
}
回调函数
public void getTime(final callBack callback){
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (timerTaskBool){
try {
flag = getVal(1);
callback.getflag(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
timer = new Timer();
timer.schedule(timerTask,0,2000);
}
界面代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@drawable/pic_bg_green"
android:layout_weight="2">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_tab"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/fan"
android:layout_marginTop="20dp"
android:id="@+id/iv_fan"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0066cc"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/f1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:layout_gravity="center"
android:textColor="#fff"
android:text="转速"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:id="@+id/tv_val"
android:textColor="#fff"
android:text="30r/s"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cc1111"
android:layout_marginTop="20dp"
android:visibility="gone"
android:id="@+id/ll_1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/pic_alarm_on"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:layout_gravity="center"
android:textColor="#fff"
android:text="报警信息"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:textColor="#fff"
android:text="转速过快"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>