File size: 9,915 Bytes
1d3d9a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
/****************************************************************
** INCLUDES
****************************************************************/
#include <stdint.h>
#include <cstdio>
#include "debug.h"
//Universal Parser V6
#include "uniparser.h"
/*
//type definition using the bit width and sign
//RNG
#include <stdlib.h>
//define the ISR routune, ISR vector, and the sei() cli() function
#include <avr/interrupt.h>
//name all the register and bit
#include <avr/io.h>
//General purpose macros
#include "at_utils.h"
//AT4809 PORT macros definitions
#include "at4809_port.h"
//
#include "global.h"
//hard coded delay
#include <util/delay.h>
//Driver for the UART communication. Provides init, ISR and buffers
#include "uart.h"
//Embedded string (TODO embedded_string.cpp is a better implementation, and I have an even better implementation in the longan nano screen driver)
#include "string_uc.h"
//Driver for the 16b multi channel timeout ISR servo driver
#include "servo.h"
*/
/****************************************************************
** DEFINITIONS
****************************************************************/
//Maximum size of a signature string
#define MAX_SIGNATURE_LENGTH 32
/****************************************************************
** FUNCTION PROTOTYPES
****************************************************************/
///----------------------------------------------------------------------
/// PARSER
///----------------------------------------------------------------------
// Handlers are meant to be called automatically when a command is decoded
// User should not call them directly
//UART Command Handlers
extern void handle_ping(void);
extern void handle_sign(void);
extern void handle_revision(void);
extern void handle_stop(void);
extern void handle_set_velocity(int8_t right_speed, int8_t left_speed);
extern void handle_set_velocity_timed(uint8_t time, int8_t right_speed, int8_t left_speed);
/****************************************************************
** GLOBAL VARS PROTOTYPES
****************************************************************/
/****************************************************************
** GLOBAL VARS
****************************************************************/
///--------------------------------------------------------------------------
/// PARSER
///--------------------------------------------------------------------------
//UART Command IDs (0 and 255 are forbidden)
#define UART_CMD_PING 1
#define UART_CMD_SIGN 2
#define UART_CMD_REVISION 3
#define UART_CMD_STOP 4
#define UART_CMD_SET_VELOCITY 5
#define UART_CMD_SET_VELOCITY_TIMED 6
//Command dictionary. Command IDs 0 and 255 are forbidden
/*
uint8_t uart_cmd[] =
{
//Ping: No action. Effect is to reset the connection timeout
UART_CMD_PING , 'P', '\0',
//Sign: Ask for board signature
UART_CMD_SIGN , 'F', '\0',
//Sign: Ask for firmware revision
UART_CMD_REVISION , 'R', 'E', 'V', '\0',
//STOP: set speed 0 to the motors
UART_CMD_STOP , 'S', 'T', 'O', 'P', '\0',
//VELOCITY: R right engine L left engine (last forever until overwritten)
UART_CMD_SET_VELOCITY , 'V', 'R', '%', 's', 'L', '%', 's', '\0',
//VELOCITY TIMED: R right engine L left engine T time of the motion
UART_CMD_SET_VELOCITY_TIMED , 'V', 'R', '%', 's', 'L', '%', 's', 'T', '%', 'u', '\0',
//Dictionary terminator
'\0'
};
*/
//Board Signature
const char *g_ps8_board_sign = "Industrious_Resonance";
//Firmware Revision
const char *g_ps8_board_revision = "2025-11-09";
//communication timeout counter
//uint8_t g_uart_timeout_cnt = 0;
//Communication timeout has been detected
//bool g_f_timeout_detected = false;
/***************************************************************************/
//! function
//! init_parser_commands | Orangebot::Uniparser &
/***************************************************************************/
//! @param parser_tmp | Orangebot::Uniparser | Parser to which commands are to be atached
//! @return bool | false = OK | true = command was invalid and was not registered
//! @brief
//! @details
/***************************************************************************/
bool init_parser_commands( Orangebot::Uniparser &i_rcl_parser )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//Return flag
bool f_ret = false;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//! Register commands and handler for the universal parser class. A masterpiece :')
//Register all UART commands with their handlers
f_ret = false;
f_ret |= i_rcl_parser.add_cmd( "P", (void *)&handle_ping );
f_ret |= i_rcl_parser.add_cmd( "F", (void *)&handle_sign );
f_ret |= i_rcl_parser.add_cmd( "REV", (void *)&handle_revision );
f_ret |= i_rcl_parser.add_cmd( "STOP", (void *)&handle_stop );
f_ret |= i_rcl_parser.add_cmd( "VR%sL%s", (void *)&handle_set_velocity );
f_ret |= i_rcl_parser.add_cmd( "T%uVR%sL%s", (void *)&handle_set_velocity_timed );
//If: Uniparser V4 failed to register a command
if (f_ret == true)
{
//TODO: signal error
}
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return f_ret;
} //End function: init_parser_commands | Orangebot::Uniparser &
/****************************************************************************
** Function
** handle_ping | void
****************************************************************************/
//! @brief Handles ping command - resets connection timeout
//! @details No action required, just resets the connection timeout
/***************************************************************************/
void handle_ping(void)
{
DENTER();
printf("EXE | %s -> Ping received\n", __FUNCTION__);
DRETURN();
return;
}
/****************************************************************************
** Function
** handle_sign | void
****************************************************************************/
//! @brief Handles signature request command
//! @details Responds with board signature string
/***************************************************************************/
void handle_sign(void)
{
DENTER();
printf("EXE | %s -> Board signature requested\n", __FUNCTION__);
printf("Board Signature: %s\n", g_ps8_board_sign);
DRETURN();
return;
}
/****************************************************************************
** Function
** handle_revision | void
****************************************************************************/
//! @brief Handles firmware revision request command
//! @details Responds with firmware revision string
/***************************************************************************/
void handle_revision(void)
{
DENTER();
printf("EXE | %s -> Firmware revision requested\n", __FUNCTION__);
printf("Firmware Revision: %s\n", g_ps8_board_revision);
DRETURN();
return;
}
/****************************************************************************
** Function
** handle_stop | void
****************************************************************************/
//! @brief Handles stop command - sets motor speeds to zero
//! @details Stops all motors by setting speed to 0
/***************************************************************************/
void handle_stop(void)
{
DENTER();
printf("EXE | %s -> Stop command received\n", __FUNCTION__);
printf("Setting motor speeds to 0\n");
DRETURN();
return;
}
/****************************************************************************
** Function
** handle_set_velocity | int16_t, int16_t
****************************************************************************/
//! @brief Handles velocity command - sets motor speeds
//! @param right_speed Speed for right motor
//! @param left_speed Speed for left motor
//! @details Sets the speed of both motors to specified values
/***************************************************************************/
void handle_set_velocity(int8_t i_s8_right_speed, int8_t i_s8_left_speed)
{
DENTER_ARG("in: Right=%d, Left=%d\n", i_s8_right_speed, i_s8_left_speed);
printf("EXE | %s -> Set velocity command\n", __FUNCTION__);
printf("Right motor speed: %d\n", i_s8_right_speed);
printf("Left motor speed: %d\n", i_s8_left_speed);
DRETURN();
return;
}
/****************************************************************************
** Function
** handle_set_velocity_timed | int16_t, int16_t, uint8_t
****************************************************************************/
//! @brief Handles timed velocity command - sets motor speeds for specific time
//! @param right_speed Speed for right motor
//! @param left_speed Speed for left motor
//! @param time Duration in seconds
//! @details Sets the speed of both motors for specified duration
/***************************************************************************/
void handle_set_velocity_timed(uint8_t time, int8_t right_speed, int8_t left_speed)
{
DENTER_ARG("in: Right=%d, Left=%d, Time=%d\n", right_speed, left_speed, time);
printf("EXE | %s -> Set velocity timed command\n", __FUNCTION__);
printf("Right motor speed: %d\n", right_speed);
printf("Left motor speed: %d\n", left_speed);
printf("Duration: %d seconds\n", time);
DRETURN();
return;
}
|