清理编码器+死代码+可读性整理: 删除ENCODER类(含UB析构), MotorController简化为PWM+GPIO裸控制器, 移除mortor_kp/ki/kd全局变量, 排除vl53l0x/zebra_detect死代码, 修复image_cv行列写反, 清理无关include
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
+7
-18
@@ -1,37 +1,26 @@
|
||||
/*
|
||||
* @Author: ilikara 3435193369@qq.com
|
||||
* @Date: 2024-10-10 14:36:47
|
||||
* @LastEditors: ilikara 3435193369@qq.com
|
||||
* @LastEditTime: 2025-03-21 10:42:00
|
||||
* @FilePath: /smartcar/lib/MotorController.h
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
* MotorController — 直流电机控制器
|
||||
*
|
||||
* 封装 PWM 调速 + GPIO 方向控制,开环占空比直驱。
|
||||
* 不包含编码器反馈和 PID 闭环(开环巡线够用)。
|
||||
*/
|
||||
#ifndef MOTOR_CONTROLLER_H
|
||||
#define MOTOR_CONTROLLER_H
|
||||
|
||||
#include "PwmController.h"
|
||||
#include "PIDController.h"
|
||||
#include "GPIO.h"
|
||||
#include "encoder.h"
|
||||
|
||||
class MotorController
|
||||
{
|
||||
public:
|
||||
MotorController(int pwmchip, int pwmnum, int gpioNum, unsigned int period_ns,
|
||||
double kp, double ki, double kd, double targetSpeed,
|
||||
int encoder_pwmNum, int encoder_gpioNum, int encoder_dir_);
|
||||
MotorController(int pwmchip, int pwmnum, int gpioNum, unsigned int period_ns);
|
||||
~MotorController(void);
|
||||
|
||||
void updateSpeed(void);
|
||||
void updateTarget(int speed);
|
||||
void updateduty(double dutyCycle);
|
||||
PIDController pidController;
|
||||
void updateduty(double dutyCycle); // dutyCycle: -100.0 ~ 100.0
|
||||
|
||||
private:
|
||||
PwmController pwmController;
|
||||
ENCODER encoder;
|
||||
GPIO directionGPIO;
|
||||
int encoder_dir;
|
||||
};
|
||||
|
||||
#endif // MOTOR_CONTROLLER_H
|
||||
#endif
|
||||
|
||||
@@ -17,20 +17,14 @@
|
||||
#include <thread>
|
||||
|
||||
#include "image_cv.h"
|
||||
#include "PIDController.h"
|
||||
#include "PwmController.h"
|
||||
#include "global.h"
|
||||
#include "frame_buffer.h"
|
||||
#include "serial.h"
|
||||
#include "control.h"
|
||||
|
||||
int CameraInit(uint8_t camera_id, double dest_fps, int width, int height);
|
||||
int CameraHandler(void);
|
||||
void cameraDeInit(void);
|
||||
|
||||
extern double kp;
|
||||
extern double ki;
|
||||
extern double kd;
|
||||
extern double g_steer_deviation;
|
||||
|
||||
#endif
|
||||
|
||||
+6
-9
@@ -1,21 +1,18 @@
|
||||
#ifndef CONTROL_H
|
||||
#define CONTROL_H
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "MotorController.h"
|
||||
#include "global.h"
|
||||
#include "serial.h"
|
||||
#include "GPIO.h"
|
||||
|
||||
// ── 电机控制接口 ──
|
||||
void ControlInit();
|
||||
void ControlUpdate(double speed, bool zebra_block);
|
||||
void ControlExit();
|
||||
|
||||
extern double mortor_kp;
|
||||
extern double mortor_ki;
|
||||
extern double mortor_kd;
|
||||
|
||||
extern GPIO mortorEN;
|
||||
// 左右电机对象(control.cpp 分配)
|
||||
extern MotorController *motorController[2];
|
||||
|
||||
// 电机使能 GPIO (73)
|
||||
extern GPIO mortorEN;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* @Author: ilikara 3435193369@qq.com
|
||||
* @Date: 2024-10-11 06:20:04
|
||||
* @LastEditors: ilikara 3435193369@qq.com
|
||||
* @LastEditTime: 2024-12-01 03:48:28
|
||||
* @FilePath: /smartcar/lib/encoder.h
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) 2024 by ilikara 3435193369@qq.com, All Rights Reserved.
|
||||
*/
|
||||
#ifndef ENCODER_H
|
||||
#define ENCODER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "GPIO.h"
|
||||
|
||||
#define PWM_BASE_ADDR 0x1611B000
|
||||
#define PWM_OFFSET 0x10
|
||||
#define LOW_BUFFER_OFFSET 0x4
|
||||
#define FULL_BUFFER_OFFSET 0x8
|
||||
#define CONTROL_REG_OFFSET 0xC
|
||||
|
||||
#define CNTR_ENABLE_BIT (1 << 0) // 计数器使能
|
||||
#define PULSE_OUT_ENABLE_BIT (1 << 3) // 脉冲输出使能(低有效)
|
||||
#define SINGLE_PULSE_BIT (1 << 4) // 单脉冲控制位
|
||||
#define INT_ENABLE_BIT (1 << 5) // 中断使能
|
||||
#define INT_STATUS_BIT (1 << 6) // 中断状态
|
||||
#define COUNTER_RESET_BIT (1 << 7) // 计数器重置
|
||||
#define MEASURE_PULSE_BIT (1 << 8) // 测量脉冲使能
|
||||
#define INVERT_OUTPUT_BIT (1 << 9) // 输出翻转使能
|
||||
#define DEAD_ZONE_ENABLE_BIT (1 << 10) // 防死区使能
|
||||
|
||||
#define LOW_BUFFER_ADDR (PWM_BASE_ADDR + LOW_BUFFER_OFFSET)
|
||||
#define FULL_BUFFER_ADDR (PWM_BASE_ADDR + FULL_BUFFER_OFFSET)
|
||||
#define CONTROL_REG_ADDR (PWM_BASE_ADDR + CONTROL_REG_OFFSET)
|
||||
|
||||
#define GPIO_PIN 73
|
||||
#define GPIO_PATH "/sys/class/gpio/gpio73/value"
|
||||
|
||||
#define PAGE_SIZE 0x10000
|
||||
|
||||
#define REG_READ(addr) (*(volatile uint32_t *)(addr))
|
||||
#define REG_WRITE(addr, val) (*(volatile uint32_t *)(addr) = (val))
|
||||
|
||||
class ENCODER
|
||||
{
|
||||
|
||||
public:
|
||||
ENCODER(int pwmNum, int gpioNum);
|
||||
~ENCODER();
|
||||
|
||||
double pulse_counter_update(void);
|
||||
|
||||
private:
|
||||
uint32_t base_addr;
|
||||
GPIO directionGPIO;
|
||||
void *low_buffer;
|
||||
void *full_buffer;
|
||||
void *control_buffer;
|
||||
void *map_register(uint32_t physical_address, size_t size);
|
||||
void PWM_Init(void);
|
||||
void reset_counter(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
+17
-12
@@ -2,18 +2,12 @@
|
||||
#define GLOBAL_H
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <atomic>
|
||||
|
||||
// ── 参数文件名 ──
|
||||
const std::string kp_file = "./kp";
|
||||
const std::string ki_file = "./ki";
|
||||
const std::string kd_file = "./kd";
|
||||
|
||||
const std::string mortor_kp_file = "./mortor_kp";
|
||||
const std::string mortor_ki_file = "./mortor_ki";
|
||||
const std::string mortor_kd_file = "./mortor_kd";
|
||||
|
||||
const std::string start_file = "./start";
|
||||
const std::string showImg_file = "./showImg";
|
||||
const std::string destfps_file = "./destfps";
|
||||
@@ -24,14 +18,25 @@ const std::string speed_file = "./speed";
|
||||
const std::string deadband_file = "./deadband";
|
||||
const std::string steer_gain_file = "./steer_gain";
|
||||
const std::string center_bias_file = "./center_bias";
|
||||
const std::string debug_file = "./debug";
|
||||
|
||||
// 从文件读取双精度值
|
||||
double readDoubleFromFile(const std::string &filename);
|
||||
bool readFlag(const std::string &filename);
|
||||
void cfg_load_all();
|
||||
|
||||
// 从文件中读取标志
|
||||
bool readFlag(const std::string &filename);
|
||||
|
||||
extern std::atomic<double> PID_rotate;
|
||||
// ── 启动时缓存的运行参数 ──
|
||||
struct CfgCache {
|
||||
double speed = 60; // 目标速度(占空比 %)
|
||||
double foresee = 80; // 前瞻行
|
||||
double zebrasee = 60; // 斑马线触发距离阈值
|
||||
double deadband = 5; // 舵机死区
|
||||
double steer_gain = 1.0; // 舵机增益
|
||||
double center_bias = 0; // 中线偏移修正
|
||||
int start = 0; // 1=启动 0=停止
|
||||
int showImg = 0; // LCD 预览开关
|
||||
int debug = 0; // debug 模式
|
||||
};
|
||||
extern CfgCache g_cfg;
|
||||
|
||||
extern double target_speed;
|
||||
|
||||
|
||||
+4
-16
@@ -1,11 +1,3 @@
|
||||
/*
|
||||
* @Author: ilikara 3435193369@qq.com
|
||||
* @Date: 2025-01-04 06:51:37
|
||||
* @LastEditors: ilikara 3435193369@qq.com
|
||||
* @LastEditTime: 2025-03-13 08:05:00
|
||||
* @FilePath: /smartcar/lib/image_main.h
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
*/
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@@ -13,16 +5,12 @@
|
||||
void image_main();
|
||||
|
||||
extern cv::Mat raw_frame;
|
||||
extern cv::Mat grayFrame;
|
||||
extern cv::Mat binarizedFrame;
|
||||
extern cv::Mat morphologyExFrame;
|
||||
extern cv::Mat track;
|
||||
|
||||
extern std::vector<int> left_line; // 左边缘列号数组
|
||||
extern std::vector<int> right_line; // 右边缘列号数组
|
||||
extern std::vector<int> mid_line; // 中线列号数组
|
||||
extern std::vector<double> left_line_filtered; // 中线列号数组
|
||||
extern std::vector<double> right_line_filtered; // 中线列号数组
|
||||
extern std::vector<double> mid_line_filtered; // 中线列号数组
|
||||
extern std::vector<int> left_line;
|
||||
extern std::vector<int> right_line;
|
||||
extern std::vector<int> mid_line;
|
||||
|
||||
extern int line_tracking_height, line_tracking_width;
|
||||
extern int line_tracking_height, line_tracking_width;
|
||||
|
||||
Reference in New Issue
Block a user