八方向运动控制优化完成
This commit is contained in:
parent
4faa9f6b35
commit
ad69c30df4
@ -91,6 +91,46 @@
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-left {
|
||||
background-color: #2196F3;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-right {
|
||||
background-color: #FF9800;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-rotate-left {
|
||||
background-color: #9C27B0;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-rotate-right {
|
||||
background-color: #607D8B;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-forward-left {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-forward-right {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-backward-left {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-backward-right {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-3px);
|
||||
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
|
||||
|
||||
@ -1,6 +1,14 @@
|
||||
// 获取按钮和状态元素
|
||||
const forwardBtn = document.getElementById('forwardBtn');
|
||||
const backwardBtn = document.getElementById('backwardBtn');
|
||||
const leftBtn = document.getElementById('leftBtn');
|
||||
const rightBtn = document.getElementById('rightBtn');
|
||||
const rotateLeftBtn = document.getElementById('rotateLeftBtn');
|
||||
const rotateRightBtn = document.getElementById('rotateRightBtn');
|
||||
const forwardLeftBtn = document.getElementById('forwardLeftBtn');
|
||||
const forwardRightBtn = document.getElementById('forwardRightBtn');
|
||||
const backwardLeftBtn = document.getElementById('backwardLeftBtn');
|
||||
const backwardRightBtn = document.getElementById('backwardRightBtn');
|
||||
const status = document.getElementById('status');
|
||||
|
||||
// 实际控制函数
|
||||
@ -9,7 +17,21 @@ function controlMotor(direction) {
|
||||
if (direction === 'stop') {
|
||||
status.innerHTML = `<p>正在停止...</p>`;
|
||||
} else {
|
||||
status.innerHTML = `<p>正在${direction === 'forward' ? '前进' : '后退'}...</p>`;
|
||||
let actionText = '';
|
||||
switch (direction) {
|
||||
case 'forward': actionText = '前进'; break;
|
||||
case 'backward': actionText = '后退'; break;
|
||||
case 'left': actionText = '左移'; break;
|
||||
case 'right': actionText = '右移'; break;
|
||||
case 'rotate_left': actionText = '左旋转'; break;
|
||||
case 'rotate_right': actionText = '右旋转'; break;
|
||||
case 'forward_left': actionText = '左前移动'; break;
|
||||
case 'forward_right': actionText = '右前移动'; break;
|
||||
case 'backward_left': actionText = '左后移动'; break;
|
||||
case 'backward_right': actionText = '右后移动'; break;
|
||||
default: actionText = '移动'; break;
|
||||
}
|
||||
status.innerHTML = `<p>正在${actionText}...</p>`;
|
||||
}
|
||||
|
||||
// 记录操作(如果正在录制)
|
||||
@ -114,6 +136,287 @@ backwardBtn.addEventListener('touchcancel', (e) => {
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左移按钮 - 支持鼠标和触摸事件
|
||||
leftBtn.addEventListener('mousedown', () => {
|
||||
// 发送左移请求
|
||||
controlMotor('left');
|
||||
});
|
||||
|
||||
leftBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
leftBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左移按钮 - 触摸事件
|
||||
leftBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送左移请求
|
||||
controlMotor('left');
|
||||
});
|
||||
|
||||
leftBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
leftBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右移按钮 - 支持鼠标和触摸事件
|
||||
rightBtn.addEventListener('mousedown', () => {
|
||||
// 发送右移请求
|
||||
controlMotor('right');
|
||||
});
|
||||
|
||||
rightBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
rightBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右移按钮 - 触摸事件
|
||||
rightBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送右移请求
|
||||
controlMotor('right');
|
||||
});
|
||||
|
||||
rightBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
rightBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左旋转按钮 - 支持鼠标和触摸事件
|
||||
rotateLeftBtn.addEventListener('mousedown', () => {
|
||||
// 发送左旋转请求
|
||||
controlMotor('rotate_left');
|
||||
});
|
||||
|
||||
rotateLeftBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
rotateLeftBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左旋转按钮 - 触摸事件
|
||||
rotateLeftBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送左旋转请求
|
||||
controlMotor('rotate_left');
|
||||
});
|
||||
|
||||
rotateLeftBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
rotateLeftBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右旋转按钮 - 支持鼠标和触摸事件
|
||||
rotateRightBtn.addEventListener('mousedown', () => {
|
||||
// 发送右旋转请求
|
||||
controlMotor('rotate_right');
|
||||
});
|
||||
|
||||
rotateRightBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
rotateRightBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右旋转按钮 - 触摸事件
|
||||
rotateRightBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送右旋转请求
|
||||
controlMotor('rotate_right');
|
||||
});
|
||||
|
||||
rotateRightBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右旋转按钮 - 触摸事件
|
||||
rotateRightBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左前按钮 - 支持鼠标和触摸事件
|
||||
forwardLeftBtn.addEventListener('mousedown', () => {
|
||||
// 发送左前移动请求
|
||||
controlMotor('forward_left');
|
||||
});
|
||||
|
||||
forwardLeftBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
forwardLeftBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左前按钮 - 触摸事件
|
||||
forwardLeftBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送左前移动请求
|
||||
controlMotor('forward_left');
|
||||
});
|
||||
|
||||
forwardLeftBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
forwardLeftBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右前按钮 - 支持鼠标和触摸事件
|
||||
forwardRightBtn.addEventListener('mousedown', () => {
|
||||
// 发送右前移动请求
|
||||
controlMotor('forward_right');
|
||||
});
|
||||
|
||||
forwardRightBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
forwardRightBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右前按钮 - 触摸事件
|
||||
forwardRightBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送右前移动请求
|
||||
controlMotor('forward_right');
|
||||
});
|
||||
|
||||
forwardRightBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
forwardRightBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左后按钮 - 支持鼠标和触摸事件
|
||||
backwardLeftBtn.addEventListener('mousedown', () => {
|
||||
// 发送左后移动请求
|
||||
controlMotor('backward_left');
|
||||
});
|
||||
|
||||
backwardLeftBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
backwardLeftBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 左后按钮 - 触摸事件
|
||||
backwardLeftBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送左后移动请求
|
||||
controlMotor('backward_left');
|
||||
});
|
||||
|
||||
backwardLeftBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
backwardLeftBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右后按钮 - 支持鼠标和触摸事件
|
||||
backwardRightBtn.addEventListener('mousedown', () => {
|
||||
// 发送右后移动请求
|
||||
controlMotor('backward_right');
|
||||
});
|
||||
|
||||
backwardRightBtn.addEventListener('mouseup', () => {
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
backwardRightBtn.addEventListener('mouseleave', () => {
|
||||
// 鼠标离开按钮时也停止
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 右后按钮 - 触摸事件
|
||||
backwardRightBtn.addEventListener('touchstart', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 发送右后移动请求
|
||||
controlMotor('backward_right');
|
||||
});
|
||||
|
||||
backwardRightBtn.addEventListener('touchend', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
backwardRightBtn.addEventListener('touchcancel', (e) => {
|
||||
e.preventDefault(); // 防止默认行为
|
||||
// 触摸被取消时停止电机
|
||||
stopMotor();
|
||||
});
|
||||
|
||||
// 停止电机函数
|
||||
function stopMotor() {
|
||||
// 记录操作(如果正在录制)
|
||||
|
||||
@ -16,10 +16,20 @@
|
||||
<!-- 移动控制区域 -->
|
||||
<div class="movement-controls">
|
||||
<div class="movement-row">
|
||||
<button class="btn btn-forward-left" id="forwardLeftBtn"><span>左前</span></button>
|
||||
<button class="btn btn-forward" id="forwardBtn"><span>前进</span></button>
|
||||
<button class="btn btn-forward-right" id="forwardRightBtn"><span>右前</span></button>
|
||||
</div>
|
||||
<div class="movement-row">
|
||||
<button class="btn btn-left" id="leftBtn"><span>左移</span></button>
|
||||
<button class="btn btn-backward" id="backwardBtn"><span>后退</span></button>
|
||||
<button class="btn btn-right" id="rightBtn"><span>右移</span></button>
|
||||
</div>
|
||||
<div class="movement-row">
|
||||
<button class="btn btn-backward-left" id="backwardLeftBtn"><span>左后</span></button>
|
||||
<button class="btn btn-rotate-left" id="rotateLeftBtn"><span>左旋转</span></button>
|
||||
<button class="btn btn-rotate-right" id="rotateRightBtn"><span>右旋转</span></button>
|
||||
<button class="btn btn-backward-right" id="backwardRightBtn"><span>右后</span></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
124
main/motor.py
124
main/motor.py
@ -19,35 +19,28 @@ signal.signal(signal.SIGINT, signal_handler)
|
||||
bus = SMBus(1)
|
||||
addr = 0x60
|
||||
|
||||
MODE1 = 0x00
|
||||
PRESCALE = 0xFE
|
||||
|
||||
bus.write_byte_data(addr, MODE1, 0x00)
|
||||
bus.write_byte_data(addr, PRESCALE, 60)
|
||||
bus.write_byte_data(addr, 0x00, 0x00)
|
||||
bus.write_byte_data(addr, 0xFE, 60)
|
||||
|
||||
# ======================
|
||||
# 电机通道映射(每个电机 IN1 / IN2)
|
||||
# 电机定义
|
||||
# ======================
|
||||
MOTOR = {
|
||||
"M1": (0, 1), # 前左
|
||||
"M2": (2, 3), # 前右
|
||||
"M3": (4, 5), # 后左
|
||||
"M4": (6, 7), # 后右
|
||||
"M1": (0, 1),
|
||||
"M2": (2, 3),
|
||||
"M3": (4, 5),
|
||||
"M4": (6, 7),
|
||||
}
|
||||
|
||||
# ======================
|
||||
# 方向修正(麦克纳姆关键)
|
||||
# 根据你 / \ + \ / 结构校正
|
||||
# ======================
|
||||
DIR = {
|
||||
"M1": 1,
|
||||
"M2": -1,
|
||||
"M3": -1,
|
||||
"M2": 1,
|
||||
"M3": 1,
|
||||
"M4": 1,
|
||||
}
|
||||
|
||||
# ======================
|
||||
# PWM输出函数
|
||||
# PWM输出
|
||||
# ======================
|
||||
def set_pwm(ch, value):
|
||||
value = max(0, min(4095, value))
|
||||
@ -58,7 +51,6 @@ def set_pwm(ch, value):
|
||||
|
||||
# ======================
|
||||
# 单电机控制
|
||||
# speed: -1 ~ 1
|
||||
# ======================
|
||||
def motor_drive(name, speed):
|
||||
in1, in2 = MOTOR[name]
|
||||
@ -69,34 +61,29 @@ def motor_drive(name, speed):
|
||||
if speed > 0:
|
||||
set_pwm(in1, pwm)
|
||||
set_pwm(in2, 0)
|
||||
|
||||
elif speed < 0:
|
||||
set_pwm(in1, 0)
|
||||
set_pwm(in2, pwm)
|
||||
|
||||
else:
|
||||
# 刹车(关键,不是0!)
|
||||
# 刹车
|
||||
set_pwm(in1, 4095)
|
||||
set_pwm(in2, 4095)
|
||||
|
||||
# ======================
|
||||
# 基础运动控制
|
||||
# 基础移动
|
||||
# ======================
|
||||
def forward(speed=0.6):
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
def backward(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
# ======================
|
||||
# 麦克纳姆左右移动
|
||||
# ======================
|
||||
def move_left(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", speed)
|
||||
@ -110,7 +97,49 @@ def move_right(speed=0.6):
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# 停止(刹车模式)
|
||||
# 旋转
|
||||
# ======================
|
||||
def rotate_left(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def rotate_right(speed=0.6):
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# 斜向移动
|
||||
# ======================
|
||||
def move_left_forward(speed=0.6):
|
||||
motor_drive("M1", 0)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", 0)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def move_right_forward(speed=0.6):
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", 0)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", 0)
|
||||
|
||||
def move_left_backward(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", 0)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", 0)
|
||||
|
||||
def move_right_backward(speed=0.6):
|
||||
motor_drive("M1", 0)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", 0)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# 停止
|
||||
# ======================
|
||||
def stop():
|
||||
motor_drive("M1", 0)
|
||||
@ -123,21 +152,24 @@ def stop():
|
||||
# ======================
|
||||
if __name__ == "__main__":
|
||||
|
||||
print("前进")
|
||||
forward(0.5)
|
||||
time.sleep(1)
|
||||
actions = [
|
||||
("前进", forward),
|
||||
("后退", backward),
|
||||
("左移", move_left),
|
||||
("右移", move_right),
|
||||
("左前", move_left_forward),
|
||||
("右前", move_right_forward),
|
||||
("左后", move_left_backward),
|
||||
("右后", move_right_backward),
|
||||
("左旋", rotate_left),
|
||||
("右旋", rotate_right),
|
||||
]
|
||||
|
||||
print("停止")
|
||||
stop()
|
||||
time.sleep(1)
|
||||
for name, func in actions:
|
||||
print(name)
|
||||
func()
|
||||
time.sleep(1)
|
||||
stop()
|
||||
time.sleep(2)
|
||||
|
||||
print("后退")
|
||||
backward(0.5)
|
||||
time.sleep(1)
|
||||
|
||||
print("停止")
|
||||
stop()
|
||||
time.sleep(1)
|
||||
|
||||
print("停止")
|
||||
stop()
|
||||
print("完成")
|
||||
|
||||
@ -26,13 +26,12 @@ bus.write_byte_data(addr, 0xFE, 60)
|
||||
# 电机定义
|
||||
# ======================
|
||||
MOTOR = {
|
||||
"M1": (0, 1), # 前左(Y轴)
|
||||
"M2": (2, 3), # 前右(Y轴)
|
||||
"M3": (4, 5), # 后左(X轴)
|
||||
"M4": (6, 7), # 后右(X轴)
|
||||
"M1": (0, 1),
|
||||
"M2": (2, 3),
|
||||
"M3": (4, 5),
|
||||
"M4": (6, 7),
|
||||
}
|
||||
|
||||
# ⚠️ 需要你微调方向(如果反了就改 ±1)
|
||||
DIR = {
|
||||
"M1": 1,
|
||||
"M2": 1,
|
||||
@ -71,46 +70,77 @@ def motor_drive(name, speed):
|
||||
set_pwm(in2, 4095)
|
||||
|
||||
# ======================
|
||||
# ⭐ 全向轮核心控制(重点)
|
||||
# vx: 左右 (-1~1)
|
||||
# vy: 前后 (-1~1)
|
||||
# w : 旋转 (-1~1)
|
||||
# ======================
|
||||
def move(vx, vy, w):
|
||||
m1 = vy + w
|
||||
m2 = vy - w
|
||||
m3 = vx + w
|
||||
m4 = vx - w
|
||||
|
||||
# 归一化(防止超限)
|
||||
max_val = max(abs(m1), abs(m2), abs(m3), abs(m4), 1)
|
||||
|
||||
motor_drive("M1", m1 / max_val)
|
||||
motor_drive("M2", m2 / max_val)
|
||||
motor_drive("M3", m3 / max_val)
|
||||
motor_drive("M4", m4 / max_val)
|
||||
|
||||
# ======================
|
||||
# 基础动作(封装)
|
||||
# 基础移动
|
||||
# ======================
|
||||
def forward(speed=0.6):
|
||||
move(0, speed, 0)
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def backward(speed=0.6):
|
||||
move(0, -speed, 0)
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
def left(speed=0.6):
|
||||
move(-speed, 0, 0)
|
||||
def move_left(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def right(speed=0.6):
|
||||
move(speed, 0, 0)
|
||||
def move_right(speed=0.6):
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# ⭐ 旋转
|
||||
# ======================
|
||||
def rotate_left(speed=0.6):
|
||||
move(0, 0, speed)
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def rotate_right(speed=0.6):
|
||||
move(0, 0, -speed)
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# ⭐ 斜向移动(新增)
|
||||
# ======================
|
||||
def move_left_forward(speed=0.6):
|
||||
motor_drive("M1", 0)
|
||||
motor_drive("M2", speed)
|
||||
motor_drive("M3", 0)
|
||||
motor_drive("M4", -speed)
|
||||
|
||||
def move_right_forward(speed=0.6):
|
||||
motor_drive("M1", speed)
|
||||
motor_drive("M2", 0)
|
||||
motor_drive("M3", -speed)
|
||||
motor_drive("M4", 0)
|
||||
|
||||
def move_left_backward(speed=0.6):
|
||||
motor_drive("M1", -speed)
|
||||
motor_drive("M2", 0)
|
||||
motor_drive("M3", speed)
|
||||
motor_drive("M4", 0)
|
||||
|
||||
def move_right_backward(speed=0.6):
|
||||
motor_drive("M1", 0)
|
||||
motor_drive("M2", -speed)
|
||||
motor_drive("M3", 0)
|
||||
motor_drive("M4", speed)
|
||||
|
||||
# ======================
|
||||
# 停止
|
||||
# ======================
|
||||
def stop():
|
||||
motor_drive("M1", 0)
|
||||
motor_drive("M2", 0)
|
||||
@ -122,25 +152,24 @@ def stop():
|
||||
# ======================
|
||||
if __name__ == "__main__":
|
||||
|
||||
print("前进")
|
||||
forward()
|
||||
time.sleep(1)
|
||||
actions = [
|
||||
("前进", forward),
|
||||
("后退", backward),
|
||||
("左移", move_left),
|
||||
("右移", move_right),
|
||||
("左前", move_left_forward),
|
||||
("右前", move_right_forward),
|
||||
("左后", move_left_backward),
|
||||
("右后", move_right_backward),
|
||||
("左旋", rotate_left),
|
||||
("右旋", rotate_right),
|
||||
]
|
||||
|
||||
print("后退")
|
||||
backward()
|
||||
time.sleep(1)
|
||||
for name, func in actions:
|
||||
print(name)
|
||||
func()
|
||||
time.sleep(1)
|
||||
stop()
|
||||
time.sleep(2)
|
||||
|
||||
print("左移")
|
||||
left()
|
||||
time.sleep(1)
|
||||
|
||||
print("右移")
|
||||
right()
|
||||
time.sleep(1)
|
||||
|
||||
print("左旋转")
|
||||
rotate_left()
|
||||
time.sleep(1)
|
||||
|
||||
print("停止")
|
||||
stop()
|
||||
print("完成")
|
||||
@ -37,6 +37,46 @@ def control():
|
||||
motor.forward(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '后退中'})
|
||||
elif direction == 'left':
|
||||
print("控制垃圾桶左移")
|
||||
motor.move_left(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '左移中'})
|
||||
elif direction == 'right':
|
||||
print("控制垃圾桶右移")
|
||||
motor.move_right(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '右移中'})
|
||||
elif direction == 'rotate_left':
|
||||
print("控制垃圾桶左旋转")
|
||||
motor.rotate_left(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '左旋转中'})
|
||||
elif direction == 'rotate_right':
|
||||
print("控制垃圾桶右旋转")
|
||||
motor.rotate_right(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '右旋转中'})
|
||||
elif direction == 'forward_left':
|
||||
print("控制垃圾桶左前移动")
|
||||
motor.move_left_forward(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '左前移动中'})
|
||||
elif direction == 'forward_right':
|
||||
print("控制垃圾桶右前移动")
|
||||
motor.move_right_forward(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '右前移动中'})
|
||||
elif direction == 'backward_left':
|
||||
print("控制垃圾桶左后移动")
|
||||
motor.move_left_backward(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '左后移动中'})
|
||||
elif direction == 'backward_right':
|
||||
print("控制垃圾桶右后移动")
|
||||
motor.move_right_backward(speed=0.6)
|
||||
# 不使用time.sleep,让电机持续运行
|
||||
return jsonify({'status': 'success', 'message': '右后移动中'})
|
||||
elif direction == 'stop':
|
||||
print("停止垃圾桶")
|
||||
motor.stop()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user