melfa_driver_node.cpp
Go to the documentation of this file.
1 // Copyright 2018 Tokyo Opensource Robotics Kyokai Association (TORK)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
20 #include <limits.h>
21 #include <pthread.h>
22 #include <sched.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/mman.h>
26 #include <ros/ros.h>
30 
34 int main (int argc, char **argv)
35 {
36  // init ROS node
37  ros::init (argc, argv, "melfa_driver");
38  ros::NodeHandle nh;
39 
40  // Parameters
41  bool realtime;
42  ros::param::param<bool>("~realtime", realtime, false);
43  double period;
44  ros::param::param<double>("~period", period, 0.0071);
45 
46  // create hardware interface
47  MelfaHW robot(period);
49 
50  // diagnostic_updater
52  updater.setHardwareID("melfa_driver");
53  updater.add("diagnose", &robot, &MelfaHW::diagnose);
54 
55  // Setup realtime scheduler
56  if (realtime)
57  {
58  struct sched_param param;
59  memset(&param, 0, sizeof(param));
60  int policy = SCHED_FIFO;
61  param.sched_priority = sched_get_priority_max(policy);
62 
63  ROS_WARN("Setting up Realtime scheduler");
64  if (sched_setscheduler(0, policy, &param) < 0) {
65  ROS_ERROR("sched_setscheduler: %s", strerror(errno));
66  ROS_ERROR("Please check you are using PREEMPT_RT kernel and set /etc/security/limits.conf");
67  exit (1);
68  }
69  if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
70  {
71  ROS_ERROR("mlockall: %s", strerror(errno));
72  exit (1);
73  }
74  }
75  // set spin rate
76  ros::Rate rate (1.0 / robot.getPeriod().toSec());
78  spinner.start ();
79 
80  // write first setting packet
81  robot.write_first ();
82 
83  while (ros::ok ())
84  {
85  // Wait for reciving a packet
86  robot.read ();
87  cm.update (ros::Time::now(), robot.getPeriod());
88  robot.write ();
89 
90  // Update diagnostics
91  updater.update();
92 
93  rate.sleep ();
94  }
95  spinner.stop ();
96  return 0;
97 }
void diagnose(diagnostic_updater::DiagnosticStatusWrapper &stat)
void setHardwareID(const std::string &hwid)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
updater
#define ROS_WARN(...)
void spinner()
ROSCPP_DECL bool ok()
static Time now()
int main(int argc, char **argv)
Main function.
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
#define ROS_ERROR(...)
void write_first(void)
ros::Duration getPeriod()


melfa_driver
Author(s): Ryosuke Tajima
autogenerated on Mon Jun 10 2019 14:04:51