Files
project_bycmd/by_cmd.c

332 lines
6.2 KiB
C
Raw Normal View History

2024-05-17 01:57:18 +08:00
#include "by_cmd.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include "by_frame.h"
#include "logc/log.h"
uint8_t cmd;
uint32_t received_data[2];
uint8_t listerning_cmd;
int received_flag;
int by_cmd_init(void)
{
log_debug("by_cmd init");
return (by_frame_init());
}
void *by_cmd_loop(void *timeout)
{
while (*(int *)timeout > 0) {
int ret = by_frame_parse(&cmd, received_data);
if (!ret && cmd == listerning_cmd) {
// log_debug("got frame cmd %2X", cmd);
break;
}
usleep(10);
}
received_flag = 1;
listerning_cmd = 0;
return NULL;
}
/**
* @brief
*s
* @param cmd
*/
int by_cmd_reg_listerning(uint8_t cmd, int timeout)
{
listerning_cmd = cmd;
received_flag = 0;
pthread_t thread_listerning;
pthread_create(&thread_listerning, NULL, by_cmd_loop, &timeout);
while (timeout--) {
if (received_flag) {
// log_debug("received done! using %fSEC", (float)timeout_cnt * 0.00005);
listerning_cmd = 0;
received_flag = 0;
return 0;
}
usleep(10);
}
listerning_cmd = 0;
received_flag = 0;
log_warn("get cmd time out");
return -1;
}
/**
* @brief x
*
* @param speed
*/
void by_cmd_send_speed_x(float speed)
{
uint8_t buff[4] = {0};
memcpy(buff, &speed, 4);
by_frame_send(0x31, buff, 4);
}
/**
* @brief y
*
* @param speed
*/
void by_cmd_send_speed_y(float speed)
{
uint8_t buff[4] = {0};
memcpy(buff, &speed, 4);
by_frame_send(0x32, buff, 4);
}
/**
* @brief
*
* @param speed
*/
void by_cmd_send_speed_omega(float speed)
{
uint8_t buff[4] = {0};
memcpy(buff, &speed, 4);
by_frame_send(0x33, buff, 4);
}
/**
* @brief x
*
* @param speed
* @param time
2024-05-17 23:45:53 +08:00
* @return 0
* @return -1
2024-05-17 01:57:18 +08:00
*/
int by_cmd_send_distance_x(float speed, uint32_t time)
{
2024-05-17 23:45:53 +08:00
uint8_t buff[8] = {0};
2024-05-17 01:57:18 +08:00
memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4);
by_frame_send(0x34, buff, 8);
return (by_cmd_reg_listerning(0x34, 1000));
}
/**
* @brief y
*
* @param speed
* @param time
* @return true
* @return false
*/
int by_cmd_send_distance_y(float speed, uint32_t time)
{
2024-05-17 23:45:53 +08:00
uint8_t buff[8] = {0};
2024-05-17 01:57:18 +08:00
memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4);
by_frame_send(0x35, buff, 8);
return (by_cmd_reg_listerning(0x35, 1000));
}
/**
* @brief omega
*
* @param speed
* @param time
* @return true
* @return false
*/
int by_cmd_send_angle_omega(float speed, uint32_t time)
{
uint8_t buff[4] = {0};
memcpy(buff, &speed, 4);
memcpy(buff + 4, &time, 4);
by_frame_send(0x36, buff, 8);
return (by_cmd_reg_listerning(0x36, 1000));
}
int by_cmd_send_reset_axis_x(void)
{
uint8_t buff[4] = {0};
by_frame_send(0x41, buff, 4);
return (by_cmd_reg_listerning(0x41, 1000));
}
int by_cmd_send_reset_axis_z(void)
{
uint8_t buff[4] = {0};
by_frame_send(0x42, buff, 4);
return (by_cmd_reg_listerning(0x42, 1000));
}
int by_cmd_send_reset_end_effector(void)
{
uint8_t buff[4] = {0};
by_frame_send(0x43, buff, 4);
return (by_cmd_reg_listerning(0x43, 1000));
2024-05-17 23:45:53 +08:00
}
2024-05-21 15:05:54 +08:00
/**
* @brief x
*
* @param speed
* @param distance
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_distance_axis_x(uint8_t speed, float distance)
{
uint8_t buff[8] = {0};
memcpy(buff, &speed, 1);
2024-05-21 15:05:54 +08:00
memcpy(buff + 1, &distance, 4);
2024-05-17 23:45:53 +08:00
by_frame_send(0x44, buff, 8);
return (by_cmd_reg_listerning(0x44, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief z
*
* @param speed
* @param distance
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_distance_axis_z(uint8_t speed, float distance)
{
uint8_t buff[8] = {0};
memcpy(buff, &speed, 1);
2024-05-21 15:05:54 +08:00
memcpy(buff + 1, &distance, 4);
2024-05-17 23:45:53 +08:00
by_frame_send(0x46, buff, 8);
return (by_cmd_reg_listerning(0x46, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief x
*
* @param speed
* @param position
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_position_axis_x(uint8_t speed, float position)
{
uint8_t buff[8] = {0};
memcpy(buff, &speed, 1);
2024-05-21 15:05:54 +08:00
memcpy(buff + 1, &position, 4);
2024-05-17 23:45:53 +08:00
by_frame_send(0x47, buff, 8);
return (by_cmd_reg_listerning(0x47, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief z
*
* @param speed
* @param position
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_position_axis_z(uint8_t speed, float position)
{
uint8_t buff[8] = {0};
memcpy(buff, &speed, 1);
2024-05-21 15:05:54 +08:00
memcpy(buff + 1, &position, 4);
2024-05-17 23:45:53 +08:00
by_frame_send(0x49, buff, 8);
return (by_cmd_reg_listerning(0x49, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief
*
* @param angle
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_angle_claw_arm(float angle)
{
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x50, buff, 4);
return (by_cmd_reg_listerning(0x50, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief
*
* @param angle
* @return int
*/
2024-05-17 23:45:53 +08:00
int by_cmd_send_angle_claw(float angle)
{
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x51, buff, 4);
return (by_cmd_reg_listerning(0x51, 1000));
}
2024-05-21 15:05:54 +08:00
/**
* @brief
*
* @param angle
* @return int
*/
int by_cmd_send_angle_camera(float angle)
{
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x52, buff, 4);
return (by_cmd_reg_listerning(0x52, 1000));
}
/**
* @brief
*
* @param angle
* @return int
*/
int by_cmd_send_angle_scoop(float angle)
{
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x53, buff, 4);
return (by_cmd_reg_listerning(0x53, 1000));
}
/**
* @brief
*
* @param angle
* @return int
*/
int by_cmd_send_angle_storage(float angle)
{
uint8_t buff[4] = {0};
memcpy(buff, &angle, 4);
by_frame_send(0x54, buff, 4);
return (by_cmd_reg_listerning(0x54, 1000));
}
2024-05-17 23:45:53 +08:00
int by_cmd_send_ranging_start(void)
{
uint8_t buff[4] = {0};
by_frame_send(0x55, buff, 4);
return (by_cmd_reg_listerning(0x55, 1000));
}
int by_cmd_recv_ranging_data(float *distance)
{
uint8_t buff[4] = {0};
by_frame_send(0x56, buff, 4);
if (!by_cmd_reg_listerning(0x56, 1000)) {
memcpy(distance, &received_data[0], 4);
return 0;
} else {
return -1;
}
2024-05-17 01:57:18 +08:00
}