修复录制与回放功能,添加新电机控制程序进行测试

This commit is contained in:
Cx330 committed 2026-04-13 15:43:48 +08:00
1 parent 9d8d7444b4
commit 4faa9f6b35
2 files changed
+176 -15

No files matched your search

+30 -15
View File
@@ -4,6 +4,7 @@ window.addEventListener('DOMContentLoaded', function() {
// 录制相关全局变量
window.isRecording = false;
window.isPlaying = false;
window.recordedActions = [];
window.recordingStartTime = 0;
@@ -21,6 +22,11 @@ window.addEventListener('DOMContentLoaded', function() {
// 录制按钮点击事件 - 支持鼠标和触摸事件
function toggleRecording() {
console.log('toggleRecording called, current isRecording:', window.isRecording);
if (window.isPlaying) {
alert('正在回放,不能录制');
return;
}
if (window.isRecording) {
// 停止录制
window.isRecording = false;
@@ -52,6 +58,11 @@ window.addEventListener('DOMContentLoaded', function() {
// 回放按钮点击事件 - 支持鼠标和触摸事件
function startPlayback() {
console.log('startPlayback called, recordedActions:', window.recordedActions);
if (window.isRecording) {
alert('请先停止录制');
return;
}
if (window.recordedActions.length === 0) {
status.innerHTML = `<p>没有可回放的操作</p>`;
console.log('No actions to playback');
@@ -61,32 +72,35 @@ window.addEventListener('DOMContentLoaded', function() {
// 保存当前录制状态并禁用录制
const originalRecordingState = window.isRecording;
window.isRecording = false;
window.isPlaying = true;
status.innerHTML = `<p>开始回放...</p>`;
console.log('Starting playback');
// 按时间顺序回放操作
let lastTimestamp = 0;
let currentTime = 0;
const totalDuration = window.recordedActions[window.recordedActions.length - 1].timestamp;
// 确保至少有一个操作
if (window.recordedActions.length > 0) {
// 执行第一个操作
console.log('Executing first action immediately:', window.recordedActions[0]);
controlMotor(window.recordedActions[0].direction);
// 执行剩余的操作
for (let i = 1; i < window.recordedActions.length; i++) {
const action = window.recordedActions[i];
const delay = action.timestamp - window.recordedActions[i-1].timestamp;
console.log('Scheduling action:', action, 'at delay:', delay);
// 执行所有操作
window.recordedActions.forEach((action, index) => {
if (index === 0) {
// 第一个操作立即执行
console.log('Executing action immediately:', action);
controlMotor(action.direction);
} else {
// 计算与前一个操作的时间差
const prevAction = window.recordedActions[index - 1];
const delay = action.timestamp - prevAction.timestamp;
currentTime += delay;
console.log('Scheduling action:', action, 'at delay:', currentTime);
setTimeout(() => {
// 调用控制函数执行操作
console.log('Executing action:', action);
controlMotor(action.direction);
}, delay);
}, currentTime);
}
}
});
// 回放完成后,确保发送停止请求
setTimeout(() => {
@@ -96,7 +110,8 @@ window.addEventListener('DOMContentLoaded', function() {
console.log('Playback completed');
// 恢复原始录制状态
window.isRecording = originalRecordingState;
}, totalDuration + 1000);
window.isPlaying = false;
}, totalDuration + 2000); // 增加延迟,确保所有动作都已执行
}
console.log('Adding click event listener to playbackBtn');