修复agent驱动程序、修复手柄路径保存逻辑、增加电机转速补偿
This commit is contained in:
1 parent
f48facde94
commit
7896e90602
7 files changed
+224
-40
No files matched your search
@@ -11,6 +11,9 @@ const backwardLeftBtn = document.getElementById('backwardLeftBtn');
|
||||
const backwardRightBtn = document.getElementById('backwardRightBtn');
|
||||
const status = document.getElementById('status');
|
||||
|
||||
// 跟踪上一个记录的动作
|
||||
let lastRecordedAction = null;
|
||||
|
||||
// 实际控制函数
|
||||
function controlMotor(direction) {
|
||||
// 根据方向设置状态消息
|
||||
@@ -34,11 +37,18 @@ function controlMotor(direction) {
|
||||
status.innerHTML = `<p>正在${actionText}...</p>`;
|
||||
}
|
||||
|
||||
// 记录操作(如果正在录制)
|
||||
// 记录操作(如果正在录制且动作发生变化)
|
||||
if (window.isRecording) {
|
||||
const timestamp = Date.now() - window.recordingStartTime;
|
||||
window.recordedActions.push({ direction, timestamp });
|
||||
console.log('Recorded action:', { direction, timestamp });
|
||||
// 检查动作是否发生变化
|
||||
if (direction !== lastRecordedAction) {
|
||||
const timestamp = Date.now() - window.recordingStartTime;
|
||||
window.recordedActions.push({ direction, timestamp });
|
||||
console.log('Recorded action:', { direction, timestamp });
|
||||
// 更新上一个记录的动作
|
||||
lastRecordedAction = direction;
|
||||
} else {
|
||||
console.log('Not recording duplicate action:', direction);
|
||||
}
|
||||
} else {
|
||||
console.log('Not recording, action:', direction);
|
||||
}
|
||||
@@ -419,11 +429,18 @@ backwardRightBtn.addEventListener('touchcancel', (e) => {
|
||||
|
||||
// 停止电机函数
|
||||
function stopMotor() {
|
||||
// 记录操作(如果正在录制)
|
||||
// 记录操作(如果正在录制且动作发生变化)
|
||||
if (window.isRecording) {
|
||||
const timestamp = Date.now() - window.recordingStartTime;
|
||||
window.recordedActions.push({ direction: 'stop', timestamp });
|
||||
console.log('Recorded action:', { direction: 'stop', timestamp });
|
||||
// 检查动作是否发生变化
|
||||
if ('stop' !== lastRecordedAction) {
|
||||
const timestamp = Date.now() - window.recordingStartTime;
|
||||
window.recordedActions.push({ direction: 'stop', timestamp });
|
||||
console.log('Recorded action:', { direction: 'stop', timestamp });
|
||||
// 更新上一个记录的动作
|
||||
lastRecordedAction = 'stop';
|
||||
} else {
|
||||
console.log('Not recording duplicate action:', 'stop');
|
||||
}
|
||||
} else {
|
||||
console.log('Not recording, action:', 'stop');
|
||||
}
|
||||
|
||||
+24
-23
@@ -5,10 +5,11 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
// 游戏手柄相关变量
|
||||
let gamepad = null;
|
||||
let lastDirection = 'stop';
|
||||
let isRecording = false;
|
||||
let isPlaying = false;
|
||||
let recordedActions = [];
|
||||
let recordingStartTime = 0;
|
||||
// 使用全局变量,与control.js和record.js保持一致
|
||||
// let isRecording = false;
|
||||
// let isPlaying = false;
|
||||
// let recordedActions = [];
|
||||
// let recordingStartTime = 0;
|
||||
|
||||
// 按键状态标志(用于检测按键按下事件)
|
||||
let recordButtonPressed = false;
|
||||
@@ -104,17 +105,17 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
if (buttons[12].pressed) { // 十字键上键 - 录制
|
||||
if (!recordButtonPressed) {
|
||||
recordButtonPressed = true;
|
||||
if (!isPlaying) {
|
||||
if (isRecording) {
|
||||
if (!window.isPlaying) {
|
||||
if (window.isRecording) {
|
||||
// 停止录制
|
||||
isRecording = false;
|
||||
status.innerHTML = `<p>录制完成,共记录 ${recordedActions.length} 个操作</p>`;
|
||||
console.log('Recording stopped, actions:', recordedActions);
|
||||
window.isRecording = false;
|
||||
status.innerHTML = `<p>录制完成,共记录 ${window.recordedActions.length} 个操作</p>`;
|
||||
console.log('Recording stopped, actions:', window.recordedActions);
|
||||
} else {
|
||||
// 开始录制
|
||||
isRecording = true;
|
||||
recordedActions = [];
|
||||
recordingStartTime = Date.now();
|
||||
window.isRecording = true;
|
||||
window.recordedActions = [];
|
||||
window.recordingStartTime = Date.now();
|
||||
status.innerHTML = '<p>开始录制...</p>';
|
||||
console.log('Recording started');
|
||||
}
|
||||
@@ -128,23 +129,23 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
if (buttons[14].pressed) { // 十字键左键 - 回放
|
||||
if (!playbackButtonPressed) {
|
||||
playbackButtonPressed = true;
|
||||
if (!isRecording && !isPlaying && recordedActions.length > 0) {
|
||||
isPlaying = true;
|
||||
if (!window.isRecording && !window.isPlaying && window.recordedActions.length > 0) {
|
||||
window.isPlaying = true;
|
||||
status.innerHTML = '<p>开始回放...</p>';
|
||||
console.log('Starting playback');
|
||||
|
||||
// 按时间顺序回放操作
|
||||
let currentTime = 0;
|
||||
const totalDuration = recordedActions[recordedActions.length - 1].timestamp;
|
||||
const totalDuration = window.recordedActions[window.recordedActions.length - 1].timestamp;
|
||||
|
||||
recordedActions.forEach((action, index) => {
|
||||
window.recordedActions.forEach((action, index) => {
|
||||
if (index === 0) {
|
||||
// 第一个操作立即执行
|
||||
console.log('Executing action immediately:', action);
|
||||
controlMotor(action.direction);
|
||||
} else {
|
||||
// 计算与前一个操作的时间差
|
||||
const prevAction = recordedActions[index - 1];
|
||||
const prevAction = window.recordedActions[index - 1];
|
||||
const delay = action.timestamp - prevAction.timestamp;
|
||||
currentTime += delay;
|
||||
|
||||
@@ -162,7 +163,7 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
controlMotor('stop');
|
||||
status.innerHTML = '<p>回放完成</p>';
|
||||
console.log('Playback completed');
|
||||
isPlaying = false;
|
||||
window.isPlaying = false;
|
||||
}, totalDuration + 2000);
|
||||
}
|
||||
}
|
||||
@@ -173,12 +174,12 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
// 如果方向改变,发送控制命令
|
||||
if (direction !== lastDirection) {
|
||||
// 如果正在录制,记录操作
|
||||
if (isRecording) {
|
||||
if (window.isRecording) {
|
||||
const action = {
|
||||
direction: direction,
|
||||
timestamp: Date.now() - recordingStartTime
|
||||
timestamp: Date.now() - window.recordingStartTime
|
||||
};
|
||||
recordedActions.push(action);
|
||||
window.recordedActions.push(action);
|
||||
}
|
||||
controlMotor(direction);
|
||||
lastDirection = direction;
|
||||
@@ -212,8 +213,8 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
}
|
||||
|
||||
// 记录操作(如果正在录制)
|
||||
if (isRecording) {
|
||||
const timestamp = Date.now() - recordingStartTime;
|
||||
if (window.isRecording) {
|
||||
const timestamp = Date.now() - window.recordingStartTime;
|
||||
// 注意:这里不再重复记录,因为已经在updateGamepad中记录了
|
||||
console.log('Recorded action:', { direction, timestamp });
|
||||
} else {
|
||||
|
||||
@@ -179,6 +179,10 @@ window.addEventListener('DOMContentLoaded', function() {
|
||||
window.isRecording = true;
|
||||
window.recordedActions = [];
|
||||
window.recordingStartTime = Date.now();
|
||||
// 重置上一个记录的动作
|
||||
if (window.lastRecordedAction !== undefined) {
|
||||
window.lastRecordedAction = null;
|
||||
}
|
||||
recordBtn.textContent = '停止录制';
|
||||
status.innerHTML = `<p>开始录制...</p>`;
|
||||
console.log('Recording started');
|
||||
|
||||
Reference in new issue
Block a user