Compare commits

...

6 Commits

Author SHA1 Message Date
bmy
5a5d35026e pref: 更改为彩色日志输出 2024-06-14 17:43:00 +08:00
bmy
e98d111322 feat: 增加转塔电机接口 2024-06-14 17:42:33 +08:00
bmy
5f6c342b86 feat: 增加蜂鸣器和灯条接口 2024-06-02 18:24:01 +08:00
bmy
07a32a247e feat: 增加命令执行日志输出 2024-05-29 21:49:14 +08:00
bmy
6b0b279835 增加生成动态库配置 2024-05-21 15:06:18 +08:00
bmy
76ea73bae2 feat: 增加接口 2024-05-21 15:05:54 +08:00
7 changed files with 336 additions and 34 deletions

View File

@@ -6,5 +6,6 @@
"by_serial.h": "c", "by_serial.h": "c",
"log.h": "c", "log.h": "c",
"termios.h": "c" "termios.h": "c"
} },
"cmake.configureOnOpen": true
} }

View File

@@ -11,10 +11,15 @@ set(TOML tomlc99/toml.c)
set(CRC16 crc16/crc16.c) set(CRC16 crc16/crc16.c)
set(LOGC logc/log.c) set(LOGC logc/log.c)
# 设置彩色日志输出
add_definitions(-DLOG_USE_COLOR)
# add_library(bycmd SHARED ${BYCMD} ${FRAME} ${CRC16}) # add_library(bycmd SHARED ${BYCMD} ${FRAME} ${CRC16})
# add_library(bycmd ${BYCMD} ${FRAME} ${CRC16}) # add_library(bycmd ${BYCMD} ${FRAME} ${CRC16})
# add_executable(${test} ${TEST_MAIN} ${FRAME}) # add_executable(${test} ${TEST_MAIN} ${FRAME})
add_executable(bycmd_test ${BYCMD} ${TOML} ${CRC16} ${LOGC} ${TEST_MAIN}) add_executable(bycmd_test ${BYCMD} ${TOML} ${CRC16} ${LOGC} ${TEST_MAIN})
add_library(bycmd SHARED ${BYCMD} ${TOML} ${CRC16} ${LOGC})
target_link_libraries(bycmd_test pthread) target_link_libraries(bycmd_test pthread)
target_link_libraries(bycmd pthread)
# target_link_libraries(${test} bycmd) # target_link_libraries(${test} bycmd)

300
by_cmd.c
View File

@@ -15,8 +15,13 @@ uint32_t received_data[2];
uint8_t listerning_cmd; uint8_t listerning_cmd;
int received_flag; int received_flag;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#define LOCKAPI() pthread_mutex_lock(&lock)
#define UNLOCKAPI() pthread_mutex_unlock(&lock)
int by_cmd_init(void) int by_cmd_init(void)
{ {
log_set_level(LOG_INFO);
log_debug("by_cmd init"); log_debug("by_cmd init");
return (by_frame_init()); return (by_frame_init());
} }
@@ -57,6 +62,7 @@ int by_cmd_reg_listerning(uint8_t cmd, int timeout)
// log_debug("received done! using %fSEC", (float)timeout_cnt * 0.00005); // log_debug("received done! using %fSEC", (float)timeout_cnt * 0.00005);
listerning_cmd = 0; listerning_cmd = 0;
received_flag = 0; received_flag = 0;
log_info("get callback done");
return 0; return 0;
} }
usleep(10); usleep(10);
@@ -65,7 +71,7 @@ int by_cmd_reg_listerning(uint8_t cmd, int timeout)
listerning_cmd = 0; listerning_cmd = 0;
received_flag = 0; received_flag = 0;
log_warn("get cmd time out"); log_warn("get callback time out");
return -1; return -1;
} }
@@ -77,10 +83,12 @@ int by_cmd_reg_listerning(uint8_t cmd, int timeout)
*/ */
void by_cmd_send_speed_x(float speed) void by_cmd_send_speed_x(float speed)
{ {
LOCKAPI();
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
by_frame_send(0x31, buff, 4); by_frame_send(0x31, buff, 4);
UNLOCKAPI();
} }
/** /**
@@ -90,10 +98,13 @@ void by_cmd_send_speed_x(float speed)
*/ */
void by_cmd_send_speed_y(float speed) void by_cmd_send_speed_y(float speed)
{ {
LOCKAPI();
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
by_frame_send(0x32, buff, 4); by_frame_send(0x32, buff, 4);
UNLOCKAPI();
} }
/** /**
@@ -103,10 +114,13 @@ void by_cmd_send_speed_y(float speed)
*/ */
void by_cmd_send_speed_omega(float speed) void by_cmd_send_speed_omega(float speed)
{ {
LOCKAPI();
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
by_frame_send(0x33, buff, 4); by_frame_send(0x33, buff, 4);
UNLOCKAPI();
} }
/** /**
@@ -119,11 +133,16 @@ void by_cmd_send_speed_omega(float speed)
*/ */
int by_cmd_send_distance_x(float speed, uint32_t time) int by_cmd_send_distance_x(float speed, uint32_t time)
{ {
LOCKAPI();
log_info("send distance_x speed:%.1f, time:%dms", speed, time * 5);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4); memcpy(buff + 4, &time, 4);
by_frame_send(0x34, buff, 8); by_frame_send(0x34, buff, 8);
return (by_cmd_reg_listerning(0x34, 1000)); ret = by_cmd_reg_listerning(0x34, 1000);
UNLOCKAPI();
return (ret);
} }
/** /**
@@ -136,11 +155,16 @@ int by_cmd_send_distance_x(float speed, uint32_t time)
*/ */
int by_cmd_send_distance_y(float speed, uint32_t time) int by_cmd_send_distance_y(float speed, uint32_t time)
{ {
LOCKAPI();
log_info("send distance_y speed:%.1f, time:%dms", speed, time * 5);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4); memcpy(buff + 4, &time, 4);
by_frame_send(0x35, buff, 8); by_frame_send(0x35, buff, 8);
return (by_cmd_reg_listerning(0x35, 1000)); ret = by_cmd_reg_listerning(0x35, 1000);
UNLOCKAPI();
return (ret);
} }
/** /**
@@ -153,101 +177,325 @@ int by_cmd_send_distance_y(float speed, uint32_t time)
*/ */
int by_cmd_send_angle_omega(float speed, uint32_t time) int by_cmd_send_angle_omega(float speed, uint32_t time)
{ {
LOCKAPI();
log_info("send angle_omega:%.1f, time:%dms", speed, time * 5);
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &speed, 4); memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4); memcpy(buff + 4, &time, 4);
by_frame_send(0x36, buff, 8); by_frame_send(0x36, buff, 8);
return (by_cmd_reg_listerning(0x36, 1000)); ret = by_cmd_reg_listerning(0x36, 1000);
UNLOCKAPI();
return (ret);
} }
int by_cmd_send_reset_axis_x(void) int by_cmd_send_reset_axis_x(void)
{ {
LOCKAPI();
log_info("send reset_axis_x");
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
by_frame_send(0x41, buff, 4); by_frame_send(0x41, buff, 4);
return (by_cmd_reg_listerning(0x41, 1000)); ret = by_cmd_reg_listerning(0x41, 1000);
UNLOCKAPI();
return (ret);
} }
int by_cmd_send_reset_axis_z(void) int by_cmd_send_reset_axis_z(void)
{ {
LOCKAPI();
log_info("send reset_axis_z");
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
by_frame_send(0x42, buff, 4); by_frame_send(0x42, buff, 4);
return (by_cmd_reg_listerning(0x42, 1000)); ret = by_cmd_reg_listerning(0x42, 1000);
UNLOCKAPI();
return (ret);
} }
int by_cmd_send_reset_end_effector(void) int by_cmd_send_reset_end_effector(void)
{ {
LOCKAPI();
log_info("send end_effector");
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
by_frame_send(0x43, buff, 4); by_frame_send(0x43, buff, 4);
return (by_cmd_reg_listerning(0x43, 1000)); ret = by_cmd_reg_listerning(0x43, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 设置 x 轴增量位置
*
* @param speed
* @param distance
* @return int
*/
int by_cmd_send_distance_axis_x(uint8_t speed, float distance) int by_cmd_send_distance_axis_x(uint8_t speed, float distance)
{ {
LOCKAPI();
log_info("send distance_axis_x speed:%d, distance:%.2f", speed, distance);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 1); memcpy(&buff[0], &speed, 1);
memcpy(buff + 4, &distance, 4); memcpy(&buff[4], &distance, 4);
by_frame_send(0x44, buff, 8); by_frame_send(0x44, buff, 8);
return (by_cmd_reg_listerning(0x44, 1000)); ret = by_cmd_reg_listerning(0x44, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 设置 z 轴增量位置
*
* @param speed
* @param distance
* @return int
*/
int by_cmd_send_distance_axis_z(uint8_t speed, float distance) int by_cmd_send_distance_axis_z(uint8_t speed, float distance)
{ {
LOCKAPI();
log_info("send distance_axis_z speed:%d, distance:%.2f", speed, distance);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 1); memcpy(&buff[0], &speed, 1);
memcpy(buff + 4, &distance, 4); memcpy(&buff[4], &distance, 4);
by_frame_send(0x46, buff, 8); by_frame_send(0x46, buff, 8);
return (by_cmd_reg_listerning(0x46, 1000)); ret = by_cmd_reg_listerning(0x46, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 发送 x 轴绝对位置
*
* @param speed
* @param position
* @return int
*/
int by_cmd_send_position_axis_x(uint8_t speed, float position) int by_cmd_send_position_axis_x(uint8_t speed, float position)
{ {
LOCKAPI();
log_info("send position_axis_x speed:%d, distance:%.2f", speed, position);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 1); memcpy(&buff[0], &speed, 1);
memcpy(buff + 4, &position, 4); memcpy(&buff[4], &position, 4);
by_frame_send(0x47, buff, 8); by_frame_send(0x47, buff, 8);
return (by_cmd_reg_listerning(0x47, 1000)); ret = by_cmd_reg_listerning(0x47, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 发送 z 轴绝对位置
*
* @param speed
* @param position
* @return int
*/
int by_cmd_send_position_axis_z(uint8_t speed, float position) int by_cmd_send_position_axis_z(uint8_t speed, float position)
{ {
LOCKAPI();
log_info("send position_axis_z speed:%d, distance:%.2f", speed, position);
int ret = 0;
uint8_t buff[8] = {0}; uint8_t buff[8] = {0};
memcpy(buff, &speed, 1); memcpy(&buff[0], &speed, 1);
memcpy(buff + 4, &position, 4); memcpy(&buff[4], &position, 4);
by_frame_send(0x49, buff, 8); by_frame_send(0x49, buff, 8);
return (by_cmd_reg_listerning(0x49, 1000)); ret = by_cmd_reg_listerning(0x49, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 设置夹爪摇臂角度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_claw_arm(float angle) int by_cmd_send_angle_claw_arm(float angle)
{ {
LOCKAPI();
log_info("send angle_claw_arm angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &angle, 4); memcpy(buff, &angle, 4);
by_frame_send(0x50, buff, 4); by_frame_send(0x50, buff, 4);
return (by_cmd_reg_listerning(0x50, 1000)); ret = by_cmd_reg_listerning(0x50, 1000);
UNLOCKAPI();
return (ret);
} }
/**
* @brief 设置夹爪角度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_claw(float angle) int by_cmd_send_angle_claw(float angle)
{ {
LOCKAPI();
log_info("send angle_claw angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
memcpy(buff, &angle, 4); memcpy(buff, &angle, 4);
by_frame_send(0x51, buff, 4); by_frame_send(0x51, buff, 4);
return (by_cmd_reg_listerning(0x51, 1000)); ret = by_cmd_reg_listerning(0x51, 1000);
UNLOCKAPI();
return (ret);
}
/**
* @brief 设置摄像头角度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_camera(float angle)
{
LOCKAPI();
log_info("send angle_camera angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x52, buff, 4);
ret = by_cmd_reg_listerning(0x52, 1000);
UNLOCKAPI();
return (ret);
}
/**
* @brief 设置顶端抓取机构角度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_scoop(float angle)
{
LOCKAPI();
log_info("send angle_scoop angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x53, buff, 4);
ret = by_cmd_reg_listerning(0x53, 1000);
UNLOCKAPI();
return (ret);
}
/**
* @brief 设置托盘角度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_storage(float angle)
{
LOCKAPI();
log_info("send angle_storage angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x54, buff, 4);
ret = by_cmd_reg_listerning(0x54, 1000);
UNLOCKAPI();
return (ret);
}
/**
* @brief 设置转轴速度
*
* @param angle
* @return int
*/
int by_cmd_send_angle_zhuan(float angle)
{
LOCKAPI();
log_info("send angle_zhuan angle:%.2f", angle);
int ret = 0;
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x55, buff, 4);
ret = by_cmd_reg_listerning(0x55, 1000);
UNLOCKAPI();
return (ret);
} }
int by_cmd_send_ranging_start(void) int by_cmd_send_ranging_start(void)
{ {
LOCKAPI();
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
by_frame_send(0x55, buff, 4); by_frame_send(0x55, buff, 4);
return (by_cmd_reg_listerning(0x55, 1000)); ret = by_cmd_reg_listerning(0x55, 1000);
UNLOCKAPI();
return (ret);
} }
int by_cmd_recv_ranging_data(float *distance) int by_cmd_recv_ranging_data(float *distance)
{ {
LOCKAPI();
int ret = 0;
uint8_t buff[4] = {0}; uint8_t buff[4] = {0};
by_frame_send(0x56, buff, 4); by_frame_send(0x56, buff, 4);
if (!by_cmd_reg_listerning(0x56, 1000)) { ret = by_cmd_reg_listerning(0x56, 1000);
if (!ret) {
memcpy(distance, &received_data[0], 4); memcpy(distance, &received_data[0], 4);
}
UNLOCKAPI();
if (!ret) {
return 0; return 0;
} else { } else {
return -1; return -1;
} }
} }
/**
* @brief 发送灯指令
*
* @param status 0-关闭 其他 - 开启
* @return int
*/
int by_cmd_send_light(uint8_t status)
{
LOCKAPI();
log_info("set light %s", (status ? "on" : "off"));
int ret = 0;
uint8_t buff[4] = {0};
buff[0] = status;
by_frame_send(0x61, buff, 4);
ret = by_cmd_reg_listerning(0x61, 1000);
UNLOCKAPI();
return (ret);
}
/**
* @brief 发送蜂鸣器指令
*
* @param status 0-关闭 其他 - 开启
* @return int
*/
int by_cmd_send_beep(uint8_t status)
{
LOCKAPI();
log_info("set beep %s", (status ? "on" : "off"));
int ret = 0;
uint8_t buff[4] = {0};
buff[0] = status;
by_frame_send(0x62, buff, 4);
ret = by_cmd_reg_listerning(0x62, 1000);
UNLOCKAPI();
return (ret);
}

View File

@@ -26,8 +26,15 @@ int by_cmd_send_position_axis_z(uint8_t speed, float position);
int by_cmd_send_angle_claw_arm(float angle); int by_cmd_send_angle_claw_arm(float angle);
int by_cmd_send_angle_claw(float angle); int by_cmd_send_angle_claw(float angle);
int by_cmd_send_angle_camera(float angle);
int by_cmd_send_angle_scoop(float angle);
int by_cmd_send_angle_storage(float angle);
int by_cmd_send_angle_zhuan(float angle);
int by_cmd_send_ranging_start(void); int by_cmd_send_ranging_start(void);
int by_cmd_recv_ranging_data(float *distance); int by_cmd_recv_ranging_data(float *distance);
int by_cmd_send_light(uint8_t status);
int by_cmd_send_beep(uint8_t status);
#endif #endif

View File

@@ -146,7 +146,7 @@ int by_frame_parse(uint8_t *cmd, uint32_t *data_array)
uint16_t crc_cal = crc16_check(frame_buff, BY_FRANE_LEN - 2); uint16_t crc_cal = crc16_check(frame_buff, BY_FRANE_LEN - 2);
if (crc_val == crc_cal) { if (crc_val == crc_cal) {
log_info("received successful"); log_debug("received successful");
// 丢掉当前帧头,下次解析时就不从该帧头开始 // 丢掉当前帧头,下次解析时就不从该帧头开始
by_frame_queue_drop(&queue_recv, 1); by_frame_queue_drop(&queue_recv, 1);
// TODO 复制数据 // TODO 复制数据
@@ -154,8 +154,8 @@ int by_frame_parse(uint8_t *cmd, uint32_t *data_array)
memcpy(data_array, &frame_buff[2], BY_FRANE_DATA_LEN); memcpy(data_array, &frame_buff[2], BY_FRANE_DATA_LEN);
return 0; return 0;
} else { } else {
log_warn("receive failed"); log_debug("receive failed");
log_warn("cal crc 0x%04X, but got 0x%04X", crc_cal, crc_val); log_debug("cal crc 0x%04X, but got 0x%04X", crc_cal, crc_val);
// 丢掉当前帧头,下次解析时就不从该帧头开始 // 丢掉当前帧头,下次解析时就不从该帧头开始
by_frame_queue_drop(&queue_recv, 1); by_frame_queue_drop(&queue_recv, 1);
return -1; return -1;

View File

@@ -5,6 +5,10 @@
* under the terms of the MIT license. See `log.c` for details. * under the terms of the MIT license. See `log.c` for details.
*/ */
#if defined(__cplusplus)
extern "C" {
#endif
#ifndef LOG_H #ifndef LOG_H
#define LOG_H #define LOG_H
@@ -28,7 +32,14 @@ typedef struct {
typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata); typedef void (*log_LockFn)(bool lock, void *udata);
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; enum {
LOG_TRACE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) #define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) #define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
@@ -47,3 +58,7 @@ int log_add_fp(FILE *fp, int level);
void log_log(int level, const char *file, int line, const char *fmt, ...); void log_log(int level, const char *file, int line, const char *fmt, ...);
#endif #endif
#if defined(__cplusplus)
}
#endif

26
test.c
View File

@@ -15,10 +15,36 @@ uint32_t buff_temp[20];
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
log_set_level(LOG_INFO);
if (by_cmd_init()) { if (by_cmd_init()) {
log_error("Program exits abnormally"); log_error("Program exits abnormally");
return -1; return -1;
} }
// by_cmd_send_distance_x(12, 100);
// by_cmd_send_angle_camera(20.0);
// by_cmd_send_position_axis_z(0x11, 100);
// by_cmd_send_distance_axis_z(15, 20);
// by_cmd_send_distance_axis_x(4, 100);
// by_cmd_send_position_axis_x(4, 0);
// by_cmd_send_angle_claw_arm(34);
// by_cmd_send_angle_claw_arm(220);
// by_cmd_send_angle_claw(27);
// sleep(1);
// by_cmd_send_distance_axis_x(4, -60);
by_cmd_send_light(1);
by_cmd_send_beep(1);
sleep(0.5);
by_cmd_send_light(0);
by_cmd_send_beep(0);
sleep(1);
by_cmd_send_light(1);
by_cmd_send_beep(1);
sleep(0.5);
by_cmd_send_light(0);
by_cmd_send_beep(0);
sleep(1);
return 0; return 0;
} }