You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
567 lines
22 KiB
567 lines
22 KiB
package com.example.rougheningapp;
|
|
|
|
import static java.lang.Integer.parseInt;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
import android.os.Bundle;
|
|
import android.text.Editable;
|
|
import android.text.TextUtils;
|
|
import android.text.TextWatcher;
|
|
import android.view.View;
|
|
import android.view.WindowManager;
|
|
import android.widget.Toast;
|
|
|
|
import com.example.rougheningapp.databinding.ActivityMainBinding;
|
|
import com.example.rougheningapp.models.BspPV;
|
|
import com.example.rougheningapp.models.DisplayPV;
|
|
import com.example.rougheningapp.services.ModbusCRC;
|
|
import com.example.rougheningapp.services.MyDataHelper;
|
|
import com.example.rougheningapp.services.PopupHelper;
|
|
import com.example.rougheningapp.services.USBSerialPortHelper;
|
|
import com.example.rougheningapp.services.ttySerialPortHelper;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
public ActivityMainBinding mainBinding;//通过Binding可以获取界面数据
|
|
//USB 串口服务
|
|
public USBSerialPortHelper serialPortHelper;
|
|
|
|
|
|
private BspPV.PV_struct_define _toSendPV;
|
|
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
public MyDataHelper myDataHelper = new MyDataHelper(this);
|
|
public PopupHelper popupHelper = new PopupHelper(this);
|
|
|
|
|
|
public static byte[] stopgetAllChData = new byte[]{0x55, 0x66, 0x01, 0x01, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) (0xf7 & 0xff), (byte) (0xe0 & 0xff)};
|
|
public static int sendStopDataCount = 0;
|
|
public String _parameters_Call_Value = "1";
|
|
public String _manual_Mode_Call_Value = "未启用";
|
|
public String _automatic_Mode_Call_Value = "未启用";
|
|
public String _Tool_Rotation_Direction_Value = "正向";
|
|
public int _operation_Mode_Call_Value_int = 0;
|
|
public int WorkDistance = 0;
|
|
public int WorkWidth = 0;
|
|
|
|
public int leftCompensation = 0;
|
|
public int rightCompensation = 0;
|
|
|
|
public int leftRightCompensationNeedToSend = 0;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
//设置全屏显示
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
|
// 加载 XML 布局(关键:将 Activity 与 activity_main.xml 绑定)
|
|
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
|
|
|
|
|
|
/* USB串口 控制区域*/
|
|
serialPortHelper = new USBSerialPortHelper(this);
|
|
serialPortHelper.intialize();
|
|
serialPortHelper.connect();
|
|
// ttySerialPortHelper.MainActivity = this;
|
|
// ttySerialPortHelper.Open();
|
|
|
|
mainBinding.setManualModeBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
|
|
// 实现界面跳转(弹出子界面):弹窗的具体逻辑实现细节见popupHelper
|
|
popupHelper.ManualModePopUp(this);
|
|
}
|
|
});
|
|
mainBinding.setAutomaticModeBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
popupHelper.AutomaticModeSetPopUp(this);
|
|
|
|
}
|
|
});
|
|
|
|
|
|
|
|
|
|
// mainBinding.setRobotSpeedBtn.setOnClickListener(new View.OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// popupHelper.RobotSpeedPopUp(this);
|
|
//
|
|
// }
|
|
// });
|
|
// mainBinding.setLaneChangeDistanceBtn.setOnClickListener(new View.OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// popupHelper.LaneChangeSelectionPopUp(this);
|
|
//
|
|
// }
|
|
// });
|
|
// mainBinding.setEndPressureBtn.setOnClickListener(new View.OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// popupHelper.EndPressureSetPopUp(this);
|
|
//
|
|
// }
|
|
// });
|
|
// mainBinding.setVerticalCalibrationBtn.setOnClickListener(new View.OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// popupHelper.VerticalCalibrationPopUp(this);
|
|
//
|
|
// }
|
|
// });
|
|
// mainBinding.mainViewRobotSpeedTxView.addTextChangedListener(new TextWatcher() {
|
|
// @Override
|
|
// public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
//
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
//
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void afterTextChanged(Editable s) {
|
|
// // 文本变化后的回调,在这里进行范围检查
|
|
// String input = s.toString();
|
|
// if (!input.isEmpty()) {
|
|
// try {
|
|
// int value = Integer.parseInt(input);
|
|
// // 检查输入值是否在 -50 到 100 范围内
|
|
// if (value < 0 || value > 20) {
|
|
// showToast("请输入 0 到 20 之间的数字");
|
|
// // 可以选择清除输入或限制在范围内
|
|
// mainBinding.mainViewRobotSpeedTxView.setText("");
|
|
// } else {
|
|
// showToast("输入有效: " + value);
|
|
// }
|
|
// } catch (NumberFormatException e) {
|
|
// showToast("请输入有效的数字");
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
//
|
|
// });
|
|
|
|
|
|
mainBinding.mainViewLaneChangeDistanceTxView.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
// 文本变化后的回调,在这里进行范围检查
|
|
String input = s.toString();
|
|
if (!input.isEmpty()) {
|
|
try {
|
|
int value = Integer.parseInt(input);
|
|
// 检查输入值是否在 -50 到 100 范围内
|
|
if (value < 1 || value > 100) {
|
|
showToast("请输入 1 到 100 之间的数字");
|
|
// 可以选择清除输入或限制在范围内
|
|
mainBinding.mainViewLaneChangeDistanceTxView.setText("");
|
|
} else {
|
|
showToast("输入有效: " + value);
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
showToast("请输入有效的数字");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
mainBinding.mainViewEndPressureTxView.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
// 文本变化后的回调,在这里进行范围检查
|
|
String input = s.toString();
|
|
if (!input.isEmpty()) {
|
|
try {
|
|
int value = Integer.parseInt(input);
|
|
// 检查输入值是否在 -50 到 100 范围内
|
|
if (value < 0 || value > 3000) {
|
|
showToast("请输入 0 到 3000 之间的数字");
|
|
// 可以选择清除输入或限制在范围内
|
|
mainBinding.mainViewEndPressureTxView.setText("");
|
|
} else {
|
|
showToast("输入有效: " + value);
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
showToast("请输入有效的数字");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
mainBinding.mainViewVerticalCalibrationTxView.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
String input = s.toString().trim();
|
|
|
|
// 1. 允许空输入(用户正在删除)
|
|
if (TextUtils.isEmpty(input)) {
|
|
return;
|
|
}
|
|
|
|
// 2. 允许合法的中间状态(如"-10.")
|
|
if (input.matches("^-?\\d*(\\.\\d*)?$")) {
|
|
// 合法格式:整数、负数、小数、小数点开头
|
|
try {
|
|
|
|
double value = Double.parseDouble(input);
|
|
// 检查输入值是否在 -50 到 100 范围内
|
|
if (value < -10 || value > 10) {
|
|
showToast("请输入 -10 到 10 之间的数字");
|
|
// 可以选择清除输入或限制在范围内
|
|
mainBinding.mainViewVerticalCalibrationTxView.setText("");
|
|
} else {
|
|
showToast("输入有效: " + value);
|
|
}
|
|
// 尝试转换为double(处理小数)
|
|
//lastValidValue = Double.parseDouble(input);
|
|
} catch (NumberFormatException e) {
|
|
// 中间状态,忽略
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
mainBinding.setToolRotationDirectionBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
popupHelper.ToolRotationDirectionPopUp(this);
|
|
}
|
|
});
|
|
|
|
|
|
mainBinding.callParametersBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
popupHelper.ParametersCallPopUp(this);
|
|
}
|
|
});
|
|
|
|
|
|
mainBinding. mainViewErrMessageBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
popupHelper.RobotErrorListPopUp(this);
|
|
}
|
|
});
|
|
|
|
|
|
myDataHelper.IntializeDataBase();
|
|
|
|
|
|
//程序初始化,读取数据库,进行界面显示
|
|
String parame = mainBinding.mainViewParametersTxView.getText().toString();
|
|
|
|
//读取数据
|
|
// 下面这一堆像是界面左边的sensor实时data显示,从PV里取值。具体怎么取,见MyDataHelper文件
|
|
int paraIndex = parseInt(parame);
|
|
List<DisplayPV> pvList = myDataHelper.getDisplayData(paraIndex);
|
|
|
|
mainBinding.mainViewManualModeTxView.setText(_manual_Mode_Call_Value);
|
|
mainBinding.mainViewAutomaticModeTxView.setText(_automatic_Mode_Call_Value);
|
|
|
|
mainBinding.mainViewLaneChangeDistanceTxView.setText(String.valueOf(pvList.get(0).getLaneChangeDistance()));
|
|
mainBinding.mainViewEndPressureTxView.setText(String.valueOf(pvList.get(0).getEndForceValue()));
|
|
|
|
WorkDistance = pvList.get(0).getWorkDistanceMeters();
|
|
WorkWidth = pvList.get(0).getWorkWidthMeters();
|
|
|
|
mainBinding.mainViewVerticalCalibrationTxView.setText(String.valueOf(pvList.get(0).getVerticalCalibration()));
|
|
mainBinding.mainViewToolRotationDirectionTxView.setText(_Tool_Rotation_Direction_Value);
|
|
|
|
leftCompensation = pvList.get(0).getLeftCompSelect();
|
|
rightCompensation = pvList.get(0).getRightCompSelect();
|
|
leftRightCompensationNeedToSend = 3;
|
|
|
|
timer.schedule(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
// 使用 Handler 或 runOnUiThread 更新 UI
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
|
|
|
|
//调试 报错处理功能
|
|
//String Value = ErrorDeocdeHelper.ErrorDeocde(0b11000000);
|
|
|
|
// 更新 UI 的代码
|
|
_toSendPV = BspPV.PV_struct_define.newBuilder().
|
|
build();
|
|
|
|
// try {
|
|
// if(mainBinding.mainViewRobotSpeedTxView.getText().toString().isEmpty() )
|
|
// {
|
|
// mainBinding.mainViewRobotSpeedFrameLayout.setBackgroundResource(R.drawable.orange_rounded_rectangle);
|
|
// }
|
|
// else {
|
|
// mainBinding.mainViewRobotSpeedFrameLayout.setBackgroundResource(R.drawable.blue_rounded_rectangle);
|
|
// _toSendPV = _toSendPV.toBuilder().setRobotSpeed(
|
|
// Integer.parseInt(String.valueOf(mainBinding.mainViewRobotSpeedTxView.getText()))
|
|
// ).build();
|
|
//
|
|
// }
|
|
// } catch (Exception e) {
|
|
//
|
|
// }
|
|
|
|
//移动速度
|
|
try {
|
|
|
|
//换道距离
|
|
if(mainBinding.mainViewLaneChangeDistanceTxView.getText().toString().isEmpty() )
|
|
{
|
|
mainBinding.mainViewLaneChangeDistanceFrameLayout.setBackgroundResource(R.drawable.orange_rounded_rectangle);
|
|
}
|
|
else {
|
|
|
|
mainBinding.mainViewLaneChangeDistanceFrameLayout.setBackgroundResource(R.drawable.blue_rounded_rectangle);
|
|
_toSendPV = _toSendPV.toBuilder().setLaneChangeDistance(
|
|
Integer.parseInt(String.valueOf(mainBinding.mainViewLaneChangeDistanceTxView.getText()))
|
|
).build();
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
//压力设定
|
|
if(mainBinding.mainViewEndPressureTxView.getText().toString().isEmpty() )
|
|
{
|
|
mainBinding.mainViewEndPressureFrameLayout.setBackgroundResource(R.drawable.orange_rounded_rectangle);
|
|
}
|
|
else {
|
|
mainBinding.mainViewEndPressureFrameLayout.setBackgroundResource(R.drawable.blue_rounded_rectangle);
|
|
_toSendPV = _toSendPV.toBuilder().setEndPushForceValue(
|
|
Integer.parseInt(String.valueOf(mainBinding.mainViewEndPressureTxView.getText()))
|
|
).build();
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
//竖直微调--小数 *10
|
|
try {
|
|
|
|
if(mainBinding.mainViewVerticalCalibrationTxView.getText().toString().isEmpty() )
|
|
{
|
|
mainBinding.mainViewVerticalCalibrationFrameLayout.setBackgroundResource(R.drawable.orange_rounded_rectangle);
|
|
}
|
|
else {
|
|
mainBinding.mainViewVerticalCalibrationFrameLayout.setBackgroundResource(R.drawable.blue_rounded_rectangle);
|
|
_toSendPV = _toSendPV.toBuilder().setVerticalCalibration(
|
|
Double.parseDouble(String.valueOf(mainBinding.mainViewVerticalCalibrationTxView.getText()))
|
|
).build();
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
// showToast(e+"等你输完我再发送吧哥们");
|
|
}
|
|
|
|
_toSendPV = _toSendPV.toBuilder()
|
|
.setWorkDistanceMeters(WorkDistance)
|
|
.setWorkWidthMeters(WorkWidth)
|
|
.build();
|
|
|
|
|
|
if(leftRightCompensationNeedToSend > 0 ) {
|
|
_toSendPV = _toSendPV.toBuilder()
|
|
.setLeftCompensation(leftCompensation)
|
|
.setRightCompensation(rightCompensation)
|
|
.build();
|
|
leftRightCompensationNeedToSend --;
|
|
|
|
showToast("下发成功");
|
|
}
|
|
// else{
|
|
//// if(mainBinding.rxLeftCompensation.getText().toString().isEmpty()){
|
|
//// mainBinding.rxLeftCompensation.setText("0.0");
|
|
//// }
|
|
//// if(mainBinding.rxRightCompensation.getText().toString().isEmpty()){
|
|
//// mainBinding.rxRightCompensation.setText("0.0");
|
|
//// }
|
|
//// leftCompensation = (int)(Double.parseDouble(mainBinding.rxLeftCompensation.getText().toString()) * 100);
|
|
//// rightCompensation = (int)(Double.parseDouble(mainBinding.rxRightCompensation.getText().toString()) * 100);
|
|
//// _toSendPV = _toSendPV.toBuilder()
|
|
//// .setLeftCompensation(leftCompensation)
|
|
//// .setRightCompensation(rightCompensation)
|
|
//// .build();
|
|
// }
|
|
|
|
|
|
|
|
String manualModeDAta = mainBinding.mainViewManualModeTxView.getText().toString();
|
|
switch (manualModeDAta) {
|
|
case "无":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(1).build();
|
|
break;
|
|
case "水平":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(2).build();
|
|
break;
|
|
case "竖直向左":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(3).build();
|
|
break;
|
|
case "竖直向右":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(4).build();
|
|
break;
|
|
}
|
|
String automaticModeDAta = mainBinding.mainViewAutomaticModeTxView.getText().toString();
|
|
switch (automaticModeDAta) {
|
|
// case "无":
|
|
// _toSendPV = _toSendPV.toBuilder().setRunMode(0).build();
|
|
// break;
|
|
case "水平":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(5).build();
|
|
break;
|
|
case "竖直向左":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(6).build();
|
|
break;
|
|
case "竖直向右":
|
|
_toSendPV = _toSendPV.toBuilder().setRunMode(7).build();
|
|
break;
|
|
}
|
|
|
|
String toolRotationDirectionDAta = mainBinding.mainViewToolRotationDirectionTxView.getText().toString();
|
|
switch (toolRotationDirectionDAta) {
|
|
case "关闭":
|
|
_toSendPV = _toSendPV.toBuilder().setToolRotationDirection(0).build();
|
|
break;
|
|
case "正向":
|
|
_toSendPV = _toSendPV.toBuilder().setToolRotationDirection(1).build();
|
|
break;
|
|
case "反向":
|
|
_toSendPV = _toSendPV.toBuilder().setToolRotationDirection(2).build();
|
|
break;
|
|
}
|
|
|
|
|
|
// _toSendPV = _toSendPV.toBuilder()
|
|
// .setRunMode(3)
|
|
// .setRobotSpeed(20)
|
|
// .setLaneChangeDistance(40)
|
|
// .build();
|
|
|
|
byte[] byteArray = _toSendPV.toByteArray();
|
|
byte[] sendbyteArray = new byte[byteArray.length + 4];
|
|
byte[] sendbyteArray3 = new byte[byteArray.length + 6];
|
|
if (byteArray.length != 0) {
|
|
System.arraycopy(byteArray, 0, sendbyteArray, 4, byteArray.length);
|
|
}
|
|
sendbyteArray[0] = (byte) 0x55;
|
|
sendbyteArray[1] = (byte) 0x55;
|
|
sendbyteArray[2] = (byte) 0x01;
|
|
sendbyteArray[3] = (byte) 0x01;
|
|
|
|
byte[] byteArray2 = ModbusCRC.calculateCRC(sendbyteArray);
|
|
|
|
System.arraycopy(sendbyteArray, 0, sendbyteArray3, 0, sendbyteArray.length);
|
|
|
|
System.arraycopy(byteArray2, 0, sendbyteArray3, sendbyteArray3.length - 2, 2);
|
|
|
|
//ttySerialPortHelper.SendData(sendbyteArray);
|
|
serialPortHelper.SendData(sendbyteArray3);
|
|
}
|
|
});
|
|
}
|
|
}, 0, 250); // 延迟 0 毫秒,每隔 1000 毫秒执行一次
|
|
|
|
|
|
}
|
|
private void showToast(String message)
|
|
{
|
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
@Override
|
|
protected void onStart() {
|
|
super.onStart();
|
|
serialPortHelper.onStart();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onStop() {
|
|
serialPortHelper.onStop();
|
|
super.onStop();
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
serialPortHelper.onResume();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
serialPortHelper.onPause();
|
|
super.onPause();
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
if (timer != null) {
|
|
timer.cancel();
|
|
}
|
|
}
|
|
|
|
|
|
}
|