cmvr_head/applications/servo_manager/include/servo_manager.h

58 lines
1.2 KiB
C
Raw Normal View History

2026-04-21 17:21:20 +08:00
//
2026-04-23 09:53:17 +08:00
// Created by Administrator on 2026/4/22.
2026-04-21 17:21:20 +08:00
//
#ifndef SERVO_MANAGER_H
#define SERVO_MANAGER_H
#include <stddef.h>
2026-04-23 09:53:17 +08:00
#include <stdint.h>
#include <string>
2026-04-21 17:21:20 +08:00
#include <vector>
2026-04-23 09:53:17 +08:00
#include "servo_manager/include/servo_control.h"
2026-04-29 09:33:16 +08:00
#include "servo_manager/include/servo_config.h"
2026-04-23 09:53:17 +08:00
#include <rtthread.h>
2026-04-21 17:21:20 +08:00
class ServoManager
{
public:
2026-04-29 09:33:16 +08:00
explicit ServoManager(const std::vector<ServoConfig>& cfg);
explicit ServoManager(const std::string& json_file_path);
2026-04-21 17:21:20 +08:00
~ServoManager();
rt_err_t init(bool enable_after_init = true, bool go_home = true);
2026-04-29 09:33:16 +08:00
ServoControl* get(size_t index);
const ServoControl* get(size_t index) const;
2026-04-23 09:53:17 +08:00
ServoControl* get(const std::string& servo_id);
const ServoControl* get(const std::string& servo_id) const;
2026-04-21 17:21:20 +08:00
2026-04-23 09:53:17 +08:00
size_t count() const;
2026-04-21 17:21:20 +08:00
2026-04-23 09:53:17 +08:00
rt_err_t start();
void stop();
2026-04-21 17:21:20 +08:00
2026-04-23 09:53:17 +08:00
void setUpdatePeriodMs(rt_uint32_t update_period_ms);
void wake();
2026-04-21 17:21:20 +08:00
private:
2026-04-23 09:53:17 +08:00
static void threadEntry(void* parameter);
void run();
2026-04-21 17:21:20 +08:00
2026-04-23 09:53:17 +08:00
int findIndexByIdNoLock(const std::string& servo_id) const;
2026-04-21 17:21:20 +08:00
private:
2026-04-23 09:53:17 +08:00
std::vector<ServoControl> controls_;
rt_mutex_t mutex_;
rt_sem_t wake_sem_;
rt_thread_t thread_;
2026-04-21 17:21:20 +08:00
2026-04-27 10:33:16 +08:00
rt_uint32_t update_period_ms_{10};
2026-04-23 09:53:17 +08:00
volatile bool running_;
2026-04-21 17:21:20 +08:00
};
#endif // SERVO_MANAGER_H