20 #include <native/task.h> 21 #include <native/timer.h> 22 #include <native/mutex.h> 23 #include <native/cond.h> 25 #include <rtdm/rtcan.h> 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <sys/ioctl.h> 43 #include <linux/can.h> 44 #include <linux/can/raw.h> 48 #include <real_time_tools/timer.hpp> 72 #define rt_fprintf fprintf 76 #define rt_printf printf 81 #define rt_dev_socket socket 85 #define rt_dev_ioctl ioctl 89 #define rt_dev_close close 93 #define rt_dev_setsockopt setsockopt 97 #define rt_dev_bind bind 101 #define rt_dev_recvmsg recvmsg 105 #define rt_dev_sendto sendto 114 #include <condition_variable> 116 #include <sys/mman.h> 150 rt_mutex_create(&rt_mutex_, NULL);
158 rt_mutex_acquire(&rt_mutex_, TM_INFINITE);
166 rt_mutex_release(&rt_mutex_);
173 class condition_variable
179 RT_COND rt_condition_variable_;
186 rt_cond_create(&rt_condition_variable_, NULL);
194 void wait(std::unique_lock<mutex> &lock)
196 rt_cond_wait(&rt_condition_variable_,
197 &lock.mutex()->rt_mutex_, TM_INFINITE);
205 rt_cond_broadcast(&rt_condition_variable_);
212 typedef xenomai::mutex
Mutex;
244 int flags,
const struct sockaddr *to,
258 for (
size_t i = 0;
true; i++)
265 std::cout <<
" Managed to send after " 266 << i <<
" attempts." << std::endl;
273 std::cout <<
"WARNING: Something went wrong with sending " 274 <<
"CAN frame, error code: " 275 << ret <<
", errno: " << errno <<
". Possibly you have " 276 <<
"been attempting to send at a rate which is too " 277 <<
"high. We keep trying" << std::flush;
295 rt_fprintf(stderr,
"rt_dev_close: %s\n", strerror(-ret));
313 std::ostringstream oss;
314 oss <<
"something went wrong with receiving " 315 <<
"CAN frame, error code: " 316 << ret <<
", errno=" << errno << std::endl;
317 throw std::runtime_error(oss.str());
328 rt_print_auto_init(1);
340 rt_task_sleep(
int(sleep_time_ms * 1000000.));
342 usleep(sleep_time_ms * 1000.);
355 return double(rt_timer_read()) / 1000000.;
358 clock_gettime(CLOCK_REALTIME, &now);
359 double current_time_ms = (double)(now.tv_sec * 1e3) + (now.tv_nsec / 1e6);
361 return current_time_ms;
372 rt_task_shadow(NULL, NULL, 0, 0);
std::mutex Mutex
Wrapper around the posix specific Mutex implementation.
Definition: os_interface.hpp:224
struct can_frame can_frame_t
xeno specific include
Definition: os_interface.hpp:59
void make_this_thread_realtime()
This methd is requiered in xenomai to create a real time thread.
Definition: os_interface.hpp:368
void sleep_ms(const double &sleep_time_ms)
This function uses eather the xenomai API or the posix one.
Definition: os_interface.hpp:337
canid_t can_id_t
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:63
double get_current_time_ms()
Get the current time in millisecond.
Definition: os_interface.hpp:352
#define rt_fprintf
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:72
#define rt_dev_close
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:89
Common include.
Definition: os_interface.hpp:123
std::condition_variable ConditionVariable
Wrapper around the posix specific ConditionVariable implementation.
Definition: os_interface.hpp:230
void close_can_device(int socket)
This function is closing a socket on the Can device.
Definition: os_interface.hpp:290
#define rt_dev_recvmsg
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:101
#define rt_dev_sendto
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:105
uint64_t nanosecs_abs_t
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:67
void send_to_can_device(int fd, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
Use the osi workspace API to communicate with the can bus.
Definition: os_interface.hpp:243
void receive_message_from_can_device(int fd, struct msghdr *msg, int flags)
Poll? a message from the CAN device.
Definition: os_interface.hpp:308
void initialize_realtime_printing()
This function is needed in xenomai to initialize the real time console display of text...
Definition: os_interface.hpp:325