优化电机控制
This commit is contained in:
parent
383de43c2f
commit
a532f03228
@ -103,19 +103,9 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
// HeadTouchManager removed
|
||||
// UI控件
|
||||
private ImageView ivFace;
|
||||
private Button btnRotateForward;
|
||||
private Button btnRotateReverse;
|
||||
private SeekBar seekBarDutyCycle;
|
||||
private TextView tvDutyCycleValue;
|
||||
private int currentDutyCyclePercent = 30; // 默认占空比30%
|
||||
private static final int PWM_PERIOD = 1000000; // 1ms周期
|
||||
|
||||
// 动画管理器
|
||||
private RobotFaceAnimationManager animationManager;
|
||||
private boolean isCalibrating = false;
|
||||
private boolean isForwardCalibration = false;
|
||||
private boolean isReverseCalibration = false;
|
||||
|
||||
|
||||
private Runnable wearDetectionRunnable = new Runnable() {
|
||||
@Override
|
||||
@ -132,45 +122,11 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_robot_face);
|
||||
|
||||
// 禁止休眠,保持屏幕常亮
|
||||
// getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
|
||||
// 初始化UI控件
|
||||
ivFace = (ImageView) this.findViewById(R.id.iv_face);
|
||||
// btnRotateForward = (Button) this.findViewById(R.id.btn_rotate_forward);
|
||||
// btnRotateReverse = (Button) this.findViewById(R.id.btn_rotate_reverse);
|
||||
// seekBarDutyCycle = (SeekBar) this.findViewById(R.id.seekbar_duty_cycle);
|
||||
// tvDutyCycleValue = (TextView) this.findViewById(R.id.tv_duty_cycle_value);
|
||||
|
||||
// // 设置占空比调整监听器
|
||||
// seekBarDutyCycle.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
// @Override
|
||||
// public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
// currentDutyCyclePercent = progress;
|
||||
// tvDutyCycleValue.setText("占空比: " + progress + "%");
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
//
|
||||
// @Override
|
||||
// public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
// });
|
||||
//
|
||||
// // 设置正转按钮点击监听器
|
||||
// btnRotateForward.setOnClickListener(v -> {
|
||||
// if (motorController != null) {
|
||||
// int dutyCycle = (int) (PWM_PERIOD * currentDutyCyclePercent / 100.0);
|
||||
// motorController.rotateVerticalDown(PWM_PERIOD, 0); // 确保垂直向下PWM关闭
|
||||
// motorController.rotateVerticalUp(PWM_PERIOD, dutyCycle);
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// // 设置反转按钮点击监听器
|
||||
// btnRotateReverse.setOnClickListener(v -> {
|
||||
// if (motorController != null) {
|
||||
// int dutyCycle = (int) (PWM_PERIOD * currentDutyCyclePercent / 100.0);
|
||||
// motorController.rotateVerticalUp(PWM_PERIOD, 0); // 确保垂直向上PWM关闭
|
||||
// motorController.rotateVerticalDown(PWM_PERIOD, dutyCycle);
|
||||
// }
|
||||
// });
|
||||
|
||||
// 初始化动画管理器
|
||||
animationManager = new RobotFaceAnimationManager(this, ivFace);
|
||||
@ -195,8 +151,6 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
handler.post(faceAnimationRunnable);
|
||||
}).start();
|
||||
|
||||
|
||||
|
||||
// 检查并请求必要的权限
|
||||
checkAndRequestPermissions();
|
||||
|
||||
@ -220,16 +174,6 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
int min = motorController.getVerticalMinAngle();
|
||||
int mid = max - min;
|
||||
Log.d(TAG,"ver max:" + max + " ver min:"+min + " mid:"+ mid);
|
||||
// while(true)
|
||||
// {
|
||||
// try {
|
||||
// angleListener.rotateToAngle(direction, direction? 15 : 15 , direction ? 35 : 12);
|
||||
// direction = !direction;
|
||||
// Thread.sleep(200);
|
||||
// }catch (InterruptedException e) {
|
||||
// Log.e(TAG, "初始化延时失败", e);
|
||||
// }
|
||||
// }
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "初始化延时失败", e);
|
||||
}
|
||||
@ -237,7 +181,6 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
|
||||
//xiaozhi
|
||||
initAudio();
|
||||
// setupListeners();
|
||||
initWebSocket();
|
||||
|
||||
}
|
||||
@ -354,6 +297,21 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
if (keyCode == HEAD_TOUCH_KEY) {
|
||||
// 处理返回键按下事件
|
||||
Log.d(TAG, "触摸了机器人的头");
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
if(motorController != null){
|
||||
int angle = motorController.getVerticalCurrentAngle();
|
||||
int max = motorController.getVerticalMaxAngle();
|
||||
int min = motorController.getVerticalMinAngle();
|
||||
int mid = max - ((max - min) / 2)+500;
|
||||
Log.d(TAG, "angle: " + angle + " mid: "+ mid);
|
||||
if(angle <= mid)
|
||||
motorController.rotateVerticalToAngle(1,100,10);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return true; // 返回true表示事件已被处理
|
||||
}
|
||||
return super.onKeyDown(keyCode, event); // 让系统继续处理其他按键事件
|
||||
@ -680,7 +638,7 @@ public class MainActivity extends AppCompatActivity implements WebSocketManager.
|
||||
|
||||
if (!isAllZero) {
|
||||
webSocketManager.sendBinaryMessage(encodedData);
|
||||
Log.d(TAG, "发送音频数据: " + encodedSize + " bytes");
|
||||
//Log.d(TAG, "发送音频数据: " + encodedSize + " bytes");
|
||||
|
||||
// 更新波形图
|
||||
// byte[] buffer = new byte[size * 2];
|
||||
|
@ -52,6 +52,7 @@ public class MotorController {
|
||||
// 垂直方向角度限制常量
|
||||
private static final String VERTICAL_MAX_ANGLE_PATH = "/sys/bus/i2c/drivers/kth5502/8-006a/max_angle";
|
||||
private static final String VERTICAL_MIN_ANGLE_PATH = "/sys/bus/i2c/drivers/kth5502/8-006a/min_angle";
|
||||
private static final String VERTICAL_CURRENT_ANGLE_PATH = "/sys/bus/i2c/drivers/kth5502/8-006a/angle";
|
||||
|
||||
// 水平电机命令常量
|
||||
public static final byte CMD_ROTATE_TO_ANGLE = 0x04; // 控制电机转动到目标角度
|
||||
@ -672,6 +673,43 @@ public class MotorController {
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制垂直电机旋转至指定角度
|
||||
* @param direction 方向:1=抬头,0=低头
|
||||
* @param dutyCycle 占空比(0-100)
|
||||
* @param angle 目标角度(度)
|
||||
* @return 是否控制成功
|
||||
*/
|
||||
/**
|
||||
* 获取当前垂直角度
|
||||
* @return 当前垂直角度值,如果传感器未开启或读取失败则返回-1
|
||||
*/
|
||||
public int getVerticalCurrentAngle() {
|
||||
// 首先检查传感器是否开启
|
||||
if (!isVerticalSensorEnabled()) {
|
||||
LogManager.e(TAG, "Cannot read vertical angle: sensor is not enabled");
|
||||
return -1;
|
||||
}
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(VERTICAL_CURRENT_ANGLE_PATH));
|
||||
String line = reader.readLine();
|
||||
reader.close();
|
||||
|
||||
if (line != null) {
|
||||
int angle = Integer.parseInt(line.trim());
|
||||
LogManager.d(TAG, "Current vertical angle: " + angle);
|
||||
return angle;
|
||||
} else {
|
||||
LogManager.e(TAG, "Failed to read vertical angle: empty response");
|
||||
return -1;
|
||||
}
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
LogManager.e(TAG, "Failed to read vertical angle: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制垂直电机旋转至指定角度
|
||||
* @param direction 方向:1=抬头,0=低头
|
||||
|
@ -83,7 +83,7 @@ public class WebSocketManager {
|
||||
|
||||
@Override
|
||||
public void onMessage(ByteBuffer bytes) {
|
||||
Log.d(TAG, "Received binary message: " + bytes.remaining() + " bytes");
|
||||
//Log.d(TAG, "Received binary message: " + bytes.remaining() + " bytes");
|
||||
byte[] data = new byte[bytes.remaining()];
|
||||
bytes.get(data);
|
||||
mainHandler.post(() -> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user