Pogosim
Loading...
Searching...
No Matches
pogosim.h
Go to the documentation of this file.
1#ifndef POGOSIM_H
2#define POGOSIM_H
3
4#include <stdbool.h>
5
6#ifdef REAL_ROBOT
7#include "pogobot.h"
8#else
9#include "spogobot.h"
10#endif
11
12#include "colormaps.h"
13
14
16#if defined(__GNUC__) || defined(__clang__)
17// Static compile-time strcmp. Only available on GCC and CLANG
18#define STATIC_STRCMP(a,b) __builtin_strcmp(a,b)
19#else
20// Normal strcmp
21#define STATIC_STRCMP(a,b) strcmp(a,b)
22#endif
23#define STRINGIFY_RAW(x) #x
24#define STRINGIFY(x) STRINGIFY_RAW(x)
25
26
27// Macros to declare the mydata pointer
28#ifdef SIMULATOR // Compiling for the simulator
29#include <stddef.h>
30
31#define DECLARE_USERDATA(UDT) \
32 extern UDT *mydata;
33
34#define REGISTER_USERDATA(UDT) \
35 size_t UserdataSize = sizeof(UDT); \
36 UDT *mydata;
37
38#define SET_CALLBACK(CALLBACK_FN, FN) \
39 CALLBACK_FN = FN;
40
41extern void (*callback_create_data_schema)(void);
42extern void (*callback_export_data)(void);
43extern void (*callback_global_setup)(void);
44extern void (*callback_global_step)(void);
45
46#else // Compiling for real robots
47
48// It is possible to specify the category of robots to compile a binary for, using the ROBOT_CATEGORY variable
49// To compile for a given robot category (e.g. "robots2"), use a command like: make clean bin ROBOT_CATEGORY=robots2
50#ifndef ROBOT_CATEGORY
51#define ROBOT_CATEGORY robots // Default to "robots" category
52#endif
53
54// On real robots, declare an extern variable for a single shared instance.
55#ifdef __cplusplus
56 // C++ version - can use constexpr and noexcept for additional optimization
57 #define DECLARE_USERDATA(UDT) \
58 extern UDT myuserdata; \
59 static inline constexpr UDT * get_mydata(void) noexcept { return &myuserdata; }
60#else
61 // C version - uses restrict keyword for optimization
62 #define DECLARE_USERDATA(UDT) \
63 extern UDT myuserdata; \
64 static inline UDT * restrict get_mydata(void) { return &myuserdata; } // The accessor returns a restrict-qualified pointer.
65 // Here we use the keyword "restrict" to ensure that the compiler automatically transform mydata->foo statements into myuserdata.foo after optimization.
66 // This increases performance by removing pointer access operations.
67#endif
68
69/* Now, use mydata as an alias for the inline function result */
70#define mydata (get_mydata())
71
72#define REGISTER_USERDATA(UDT) \
73 UDT myuserdata = {0};
74
75#define SET_CALLBACK(CALLBACK_FN, FN)
76
77void user_init(void);
78void user_step(void);
79
80#endif // SIMULATOR
81
82
83#ifdef __cplusplus
84extern "C" {
85#endif
86
87extern uint32_t pogobot_ticks;
88
89extern uint8_t main_loop_hz;
91extern void (*msg_rx_fn)(message_t *);
92extern bool (*msg_tx_fn)(void);
93extern int8_t error_codes_led_idx;
96extern uint32_t _current_time_milliseconds;
97extern uint32_t _error_code_initial_time;
98
99extern uint8_t percent_msgs_sent_per_ticks;
100extern uint32_t nb_msgs_sent;
101extern uint32_t nb_msgs_recv;
102
107
108
109#define GET_MACRO_START(_1, _2, _3, NAME, ...) NAME
110
111#ifdef SIMULATOR // Compiling for the simulator
112const char* get_current_robot_category(void);
113bool current_robot_category_is(const char* category);
114
115void _pogobot_start(void (*user_init)(void), void (*user_step)(void), const char *object_category);
116#define pogobot_start_2(user_init, user_step) \
117 _pogobot_start((user_init), (user_step), "robots")
118
119#define pogobot_start_3(user_init, user_step, object_category) \
120 _pogobot_start((user_init), (user_step), (object_category))
121
122#define pogobot_start(...) GET_MACRO_START(__VA_ARGS__, pogobot_start_3, pogobot_start_2)(__VA_ARGS__)
123
124#else // Compiling for real robots
125#define get_current_robot_category() (STRINGIFY(ROBOT_CATEGORY))
126#define current_robot_category_is(category) (STATIC_STRCMP(get_current_robot_category(), category)==0)
127
128void _pogobot_start(void (*user_init)(void), void (*user_step)(void));
129#define pogobot_start_2(user_init, user_step) \
130 do { \
131 if (current_robot_category_is("robots")) { \
132 _pogobot_start((user_init), (user_step)); \
133 } \
134 } while (0)
135
136#define pogobot_start_3(user_init, user_step, object_category) \
137 do { \
138 if (current_robot_category_is(object_category)) { \
139 _pogobot_start((user_init), (user_step)); \
140 } \
141 } while (0)
142
143#define pogobot_start(...) GET_MACRO_START(__VA_ARGS__, pogobot_start_3, pogobot_start_2)(__VA_ARGS__)
144
145#endif
146
147void clear_IR_buffers(void);
148
149void pogo_main_loop_step(void (*user_step)(void));
150uint32_t current_time_milliseconds(void);
152
158void default_walls_user_init(void);
164void default_walls_user_step(void);
165
166#ifdef __cplusplus
167}
168#endif
169
170
171#endif // POGOSIM_H
172
173// MODELINE "{{{1
174// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
175// vim:foldmethod=marker
void(* msg_rx_fn)(message_t *)
Definition pogosim.c:14
time_reference_t _global_timer
Definition pogosim.c:20
uint32_t nb_msgs_recv
Definition pogosim.c:25
uint8_t main_loop_hz
Definition pogosim.c:12
void(* callback_create_data_schema)(void)
Definition pogosim.c:6
uint32_t pogobot_ticks
Definition pogosim.c:17
void(* callback_global_step)(void)
Definition pogosim.c:9
uint8_t max_nb_processed_msg_per_tick
Definition pogosim.c:13
uint32_t _current_time_milliseconds
Definition pogosim.c:18
uint32_t nb_msgs_sent
Definition pogosim.c:24
bool(* msg_tx_fn)(void)
Definition pogosim.c:15
void(* callback_global_setup)(void)
Definition pogosim.c:8
uint8_t percent_msgs_sent_per_ticks
Definition pogosim.c:23
int8_t error_codes_led_idx
Definition pogosim.c:16
uint32_t _error_code_initial_time
Definition pogosim.c:19
time_reference_t timer_main_loop
Definition pogosim.c:21
void(* callback_export_data)(void)
Definition pogosim.c:7
const char * get_current_robot_category(void)
Definition pogosim_sim.cpp:17
error_code_t
Definition pogosim.h:103
@ error_code_t_last_entry
Definition pogosim.h:105
@ ERROR_TIME_OVERFLOW
Definition pogosim.h:104
uint32_t current_time_milliseconds(void)
Definition pogosim.c:78
void clear_IR_buffers(void)
Definition pogosim.c:28
void default_walls_user_step(void)
Main control loop for the Pogowalls.
Definition pogosim.c:189
void default_walls_user_init(void)
Initialization function for the pogowalls.
Definition pogosim.c:169
void display_led_error_code(error_code_t const c)
Definition pogosim.c:84
bool current_robot_category_is(const char *category)
Definition pogosim_sim.cpp:21
void pogo_main_loop_step(void(*user_step)(void))
Definition pogosim.c:96
void _pogobot_start(void(*user_init)(void), void(*user_step)(void), const char *object_category)
Definition pogosim_sim.cpp:7
Definition spogobot.h:205
Definition spogobot.h:877