Browse Source

只保留一个定时器,将其周期延迟修改为100ms

master
LIN\54376 1 month ago
parent
commit
45a7d685a8
  1. 2
      .gradle/config.properties
  2. 4
      .idea/deploymentTargetSelector.xml
  3. 87
      app/src/main/java/com/example/fivewheel/MainActivity.java
  4. 32
      app/src/main/res/layout/activity_main.xml

2
.gradle/config.properties

@ -1,2 +1,2 @@
#Mon Dec 22 02:41:36 CST 2025 #Thu Jan 08 07:06:27 CST 2026
java.home=F\:\\Program Files\\Android\\Android Studio\\jbr java.home=F\:\\Program Files\\Android\\Android Studio\\jbr

4
.idea/deploymentTargetSelector.xml

@ -4,10 +4,10 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2025-12-19T02:11:32.151250600Z"> <DropdownSelection timestamp="2025-12-23T01:25:16.652477600Z">
<Target type="DEFAULT_BOOT"> <Target type="DEFAULT_BOOT">
<handle> <handle>
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\asus\.android\avd\Medium_Phone.avd" /> <DeviceId pluginId="Default" identifier="serial=d;connection=76beca8a" />
</handle> </handle>
</Target> </Target>
</DropdownSelection> </DropdownSelection>

87
app/src/main/java/com/example/fivewheel/MainActivity.java

@ -452,9 +452,16 @@ public class MainActivity extends AppCompatActivity {
//设置自动工作按钮的点击监听器 //设置自动工作按钮的点击监听器
mainBinding.btnBindAutoWork.setOnClickListener(v -> { mainBinding.btnBindAutoWork.setOnClickListener(v -> {
Robot_AutoWork = 1; // 标志位置 1 if (Robot_AutoWork == 0) {
updateAutoWorkButtonStyle(); // 变色 // 当前是停止状态 -> 切换为开启
saveOperationLog("开启自动工作模式"); Robot_AutoWork = 1;
saveOperationLog("开启自动工作模式");
} else {
// 当前是开启状态 -> 切换为停止(取消绑定)
Robot_AutoWork = 0;
saveOperationLog("停止自动工作模式");
}
updateAutoWorkButtonStyle(); // 更新按钮样式和文字
}); });
@ -498,14 +505,14 @@ public class MainActivity extends AppCompatActivity {
deleteOldLogs(); deleteOldLogs();
/* NodePlayer 播放视频区域*/ /* NodePlayer 播放视频区域*/
NodePlayer nodePlayer0 = new NodePlayer(this); //NodePlayer nodePlayer0 = new NodePlayer(this);
// NodePlayer nodePlayer1=new NodePlayer(this); // NodePlayer nodePlayer1=new NodePlayer(this);
//String address0 = "rtsp://192.168.1.168:8554/0"; //String address0 = "rtsp://192.168.1.168:8554/0";
// String address1 = "rtsp://192.168.1.169:8554/0"; // String address1 = "rtsp://192.168.1.169:8554/0";
//String addressTest = "rtsp://rtspstream:yEhp9qzAqveM9kHIE7GcL@zephyr.rtsp.stream/movie"; //String addressTest = "rtsp://rtspstream:yEhp9qzAqveM9kHIE7GcL@zephyr.rtsp.stream/movie";
String address0 = "rtsp://192.168.144.25:8554/main.264"; //String address0 = "rtsp://192.168.144.25:8554/main.264";
/**/ /**/
//VideoPlayerHelper.startVedio(+mainBinding.nodePlayerView0,nodePlayer0,address0); //VideoPlayerHelper.startVedio(+mainBinding.nodePlayerView0,nodePlayer0,address0);
// VideoPlayerHelper.startVedio(mainBinding.nodePlayerView1,nodePlayer1,address1); // VideoPlayerHelper.startVedio(mainBinding.nodePlayerView1,nodePlayer1,address1);
@ -524,7 +531,6 @@ public class MainActivity extends AppCompatActivity {
ModbusRtuSlaveService.ModbusRtuSlaveServiceIntialize(serialPortHelper); ModbusRtuSlaveService.ModbusRtuSlaveServiceIntialize(serialPortHelper);
} }
int UARTSendCount = 0; int UARTSendCount = 0;
public int USBSerialPortReceivedTimeCounter = 0; public int USBSerialPortReceivedTimeCounter = 0;
@ -534,6 +540,9 @@ public class MainActivity extends AppCompatActivity {
serialPortHelper.onStart(); serialPortHelper.onStart();
UARTSendCount = 0; UARTSendCount = 0;
if (timer == null) {
timer = new Timer();
}
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -552,13 +561,25 @@ public class MainActivity extends AppCompatActivity {
ModbusRtuSlaveService.holdingRegisters[18]= 1;//在线 ModbusRtuSlaveService.holdingRegisters[18]= 1;//在线
} }
// 使用安全的发送值获取方法(只在编辑完成后发送新值) // 使用安全的发送值获取方法(只在编辑完成后发送新值)
int chgLength = getSafeChgLengthForSending();
double manualSpeed = getSafeManualSpeedForSending();
double autoSpeed = getSafeAutoSpeedForSending();
int force = getSafeForceForSending();
// 在发送前进行范围限制(二次保障)
chgLength = Math.max(0, Math.min(1000, chgLength));
manualSpeed = Math.max(0, Math.min(30, manualSpeed));
autoSpeed = Math.max(0, Math.min(30, autoSpeed));
force = Math.max(300, Math.min(3000, force));
_toSendPV = _toSendPV.toBuilder() _toSendPV = _toSendPV.toBuilder()
.setRobotChgLength(getSafeInt(mainBinding.tvRobotChgLength, 0)) .setRobotChgLength(chgLength)
.setRobotAutoSpeedBase(getSafeDouble(mainBinding.tvRobotAutoSpeed, 0.0)) .setRobotAutoSpeedBase(autoSpeed)
.setRobotManualSpeedBase(getSafeDouble(mainBinding.tvRobotManualSpeed, 0.0)) .setRobotManualSpeedBase(manualSpeed)
.setRobotForce(getSafeInt(mainBinding.tvRobotForce, 0)) .setRobotLaneChangeDirection(laneChangeDirection)
.setRobotForce(force)
.setRobotAutoWork(Robot_AutoWork)
.setTimeStamp(System.currentTimeMillis()) .setTimeStamp(System.currentTimeMillis())
.build(); .build();
@ -574,6 +595,13 @@ public class MainActivity extends AppCompatActivity {
AndroidMCUCommunicationMethod = CommunicationMethond.Wired; AndroidMCUCommunicationMethod = CommunicationMethond.Wired;
} }
// 根据当前的连接方式更新 UI 显示
if (AndroidMCUCommunicationMethod == CommunicationMethond.Wired) {
mainBinding.tvRobotConnect.setText("有线");
} else {
mainBinding.tvRobotConnect.setText("无线");
}
if (AndroidMCUCommunicationMethod == CommunicationMethond.Wireless) { if (AndroidMCUCommunicationMethod == CommunicationMethond.Wireless) {
//若sdk有数据返回,则停止发送 //若sdk有数据返回,则停止发送
@ -595,6 +623,34 @@ public class MainActivity extends AppCompatActivity {
if (ttySerialPortHelper.IsAllButtonSendBack == false) { if (ttySerialPortHelper.IsAllButtonSendBack == false) {
ttySerialPortHelper.SendData(ttySerialPortHelper.getAllChData_50Hz); ttySerialPortHelper.SendData(ttySerialPortHelper.getAllChData_50Hz);
} }
}else
{
//无线模式发送
try {
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(sendByteArray3);
runOnUiThread(() -> checkMotorErrors());
} catch (Exception e) {
android.util.Log.e("MainActivity", "Error in startSending timer task", e);
}
} }
@ -604,7 +660,7 @@ public class MainActivity extends AppCompatActivity {
} }
}, 0, 250); // 延迟 0 毫秒,每隔 1000 毫秒执行一次 }, 0, 100); // 延迟 0 毫秒,每隔 1000 毫秒执行一次
} }
@ -629,7 +685,7 @@ public class MainActivity extends AppCompatActivity {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
serialPortHelper.onResume(); serialPortHelper.onResume();
// startSending();
} }
@Override @Override
@ -707,7 +763,6 @@ public class MainActivity extends AppCompatActivity {
.setRobotLaneChangeDirection(laneChangeDirection) .setRobotLaneChangeDirection(laneChangeDirection)
.setRobotForce(force) .setRobotForce(force)
.setRobotAutoWork(Robot_AutoWork) .setRobotAutoWork(Robot_AutoWork)
.setTimeStamp(System.currentTimeMillis())
.build(); .build();
@ -785,11 +840,15 @@ public class MainActivity extends AppCompatActivity {
// //
private void updateAutoWorkButtonStyle() { private void updateAutoWorkButtonStyle() {
if (Robot_AutoWork == 1) { if (Robot_AutoWork == 1) {
// 已开启自动模式:显示高亮颜色,文字变为“取消”
mainBinding.btnBindAutoWork.setText("取消自动作业");
mainBinding.btnBindAutoWork.setTextColor( mainBinding.btnBindAutoWork.setTextColor(
ContextCompat.getColor(this, R.color.mediumvioletred)); ContextCompat.getColor(this, R.color.mediumvioletred));
mainBinding.btnBindAutoWork.setBackgroundResource(R.drawable.blue_rounded_rectangle); mainBinding.btnBindAutoWork.setBackgroundResource(R.drawable.blue_rounded_rectangle);
} else { } else {
// 未开启自动模式:显示默认颜色,文字变为“绑定”
mainBinding.btnBindAutoWork.setText("绑定自动作业");
mainBinding.btnBindAutoWork.setTextColor( mainBinding.btnBindAutoWork.setTextColor(
ContextCompat.getColor(this, R.color.deepskyblue)); ContextCompat.getColor(this, R.color.deepskyblue));
mainBinding.btnBindAutoWork.setBackgroundResource(R.drawable.blue_rounded_rectangle); mainBinding.btnBindAutoWork.setBackgroundResource(R.drawable.blue_rounded_rectangle);

32
app/src/main/res/layout/activity_main.xml

@ -40,7 +40,7 @@
android:layout_gravity="center" android:layout_gravity="center"
android:columnCount="1" android:columnCount="1"
android:orientation="vertical" android:orientation="vertical"
android:rowCount="9"> android:rowCount="10">
<!--机器人作业状态--> <!--机器人作业状态-->
<LinearLayout <LinearLayout
@ -320,6 +320,36 @@
android:textStyle="bold" /> android:textStyle="bold" />
</LinearLayout> </LinearLayout>
<!--机器人连接方式-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:text="连 接"
android:textColor="@color/midnightblue"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvRobotConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="@color/midnightblue"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<!--左电机错误信息--> <!--左电机错误信息-->
<FrameLayout <FrameLayout
android:id="@+id/mainViewErrMessageFrameLayout" android:id="@+id/mainViewErrMessageFrameLayout"

Loading…
Cancel
Save