Initial commit: SmartCar Framework v0.1 — 龙芯2K0300智能车开发框架\n\n- HAL: GPIO/PWM/Encoder/Framebuffer 驱动\n- Control: PID/IMU/Motor/Servo 控制\n- Vision: HSV双Otsu→4点标定IPM→洪泛填充→逐行搜线\n- Strategy: 三区前瞻偏差+速度策略\n- Debug: 文件热调参+LCD预览+cv截帧\n- Scheduler: 5ms timerfd+epoll 中央调度器
This commit is contained in:
57
hal/pwm.cpp
Normal file
57
hal/pwm.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "pwm.hpp"
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
PWM::PWM(int chip, int channel, unsigned int period_ns)
|
||||
: _chip(chip), _channel(channel), _period_ns(period_ns), _exported(false)
|
||||
{
|
||||
_export();
|
||||
setPeriod(period_ns);
|
||||
}
|
||||
|
||||
PWM::~PWM() { _unexport(); }
|
||||
|
||||
std::string PWM::_basePath() const
|
||||
{
|
||||
return "/sys/class/pwm/pwmchip" + std::to_string(_chip) +
|
||||
"/pwm" + std::to_string(_channel);
|
||||
}
|
||||
|
||||
void PWM::_export()
|
||||
{
|
||||
std::ofstream f("/sys/class/pwm/pwmchip" + std::to_string(_chip) + "/export");
|
||||
if (f.is_open()) { f << _channel; _exported = true; }
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
void PWM::_unexport()
|
||||
{
|
||||
std::ofstream f("/sys/class/pwm/pwmchip" + std::to_string(_chip) + "/unexport");
|
||||
if (f.is_open()) f << _channel;
|
||||
}
|
||||
|
||||
void PWM::setDutyCycle(unsigned int duty_ns)
|
||||
{
|
||||
std::ofstream f(_basePath() + "/duty_cycle");
|
||||
if (f.is_open()) f << duty_ns;
|
||||
}
|
||||
|
||||
void PWM::setPeriod(unsigned int period_ns)
|
||||
{
|
||||
_period_ns = period_ns;
|
||||
std::ofstream f(_basePath() + "/period");
|
||||
if (f.is_open()) f << period_ns;
|
||||
}
|
||||
|
||||
void PWM::enable(bool on)
|
||||
{
|
||||
std::ofstream f(_basePath() + "/enable");
|
||||
if (f.is_open()) f << (on ? 1 : 0);
|
||||
}
|
||||
|
||||
void PWM::setPolarity(const char* pol)
|
||||
{
|
||||
std::ofstream f(_basePath() + "/polarity");
|
||||
if (f.is_open()) f << pol;
|
||||
}
|
||||
Reference in New Issue
Block a user