21 #include <sys/socket.h> 22 #include <linux/sockios.h> 23 #include <linux/can.h> 26 #include "geometry_msgs/Twist.h" 33 #define MOTOR_ENABLE_BIT 2 34 #define EMERGENCY_STOP_BIT 1 35 #define INDICATOR_BIT 0x80 37 #define EMERGENCY_STOP_SCANNER_BIT 1 38 #define KEY_SWITCH_STATUS_BIT 2 39 #define EMERGENCY_STOP_BUTTON_BIT 4 40 #define SCANNER_WEAK_BIT 8 41 #define EXTRA_BUTTON_BIT 32 42 #define SCANNER_OSSD2_BIT 0x10 55 ROS_ERROR(
"BaseController: AX10420_Init() failed, error code %d", ret);
62 unsigned short outputleft;
63 unsigned short outputright;
64 struct can_frame frame;
68 outputleft=(
unsigned short)(vleft/max_speed*0x7f+0x7f);
69 ROS_DEBUG(
"BaseController: outputleft = %i", outputleft);
72 outputright=(
unsigned short)(vright/max_speed*0x7f+0x7f);
73 ROS_DEBUG(
"BaseController: outputright = %i", outputright);
78 frame.data[0] = outputright & 0xff;
79 frame.data[1] = outputright >> 8;
80 frame.data[2] = outputleft & 0xff;
81 frame.data[3] = outputleft >> 8;
89 unsigned char outbyte = 0;
92 ROS_DEBUG(
"BaseController: Motor enable.");
96 ROS_DEBUG(
"BaseController: Disable emergency stop.");
102 ROS_ERROR(
"BaseController: AX10420_SetOutput() failed, error code %d", ret);
112 boost::mutex::scoped_lock scoped_lock(
mutex);
115 next = 100 * (
cmd.linear.x - (
cmd.angular.z*0.3315))*speed;
119 next = 100 * (
cmd.linear.x + (
cmd.angular.z*0.3315))*speed;
124 return velocity_old * 0.40 + next * 0.60;
144 float max_speed = 612;
145 bool motorEnabled =
false;
152 double right_adapter = 0;
153 double left_adapter = 0;
168 ROS_DEBUG(
"BaseController: 1. vleft: %f, vright: %f", vleft, vright);
183 if ((vleft != 0) || (vright != 0))
187 vright += right_adapter;
190 vleft += left_adapter;
192 ROS_DEBUG(
"BaseController: 2. vleft: %f, vright: %f", vleft, vright);
196 vleft = std::min(vleft, max_speed);
197 vleft = std::max(vleft, -max_speed);
198 vright = std::min(vright, max_speed);
199 vright = std::max(vright, -max_speed);
206 else if (motorEnabled)
208 ROS_DEBUG(
"BaseController: Motor disabled");
210 motorEnabled =
false;
215 ROS_INFO(
"BaseController: Started successfully. With speedup x%f\n", speed);
228 double adapter_value = 0;
229 double difference = std::abs(std::abs(required_velocity) - std::abs(real_velocity*100.
f));
230 if(required_velocity > 0)
232 adapter_value = difference/10.f;
236 adapter_value = -difference/10.f;
238 ROS_DEBUG(
"BaseController: difference %f, adapter %f", difference, adapter);
240 if(std::abs(required_velocity) > std::abs(real_velocity*100.f))
242 adapter += adapter_value;
246 adapter -= adapter_value;
248 if(std::abs(adapter) > std::abs(required_velocity/2))
250 adapter = required_velocity/2;
258 boost::mutex::scoped_lock scoped_lock(
mutex);
int AX10420_OpenDevice(const char *device_name)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int AX10420_Init(int fd, tAXGroup group, tAXIOConfigure port_a, tAXIOConfigure port_b, tAXIOConfigure port_c_upper, tAXIOConfigure port_c_lower)
void writeDataToCan(float vleft, float vright, float max_speed)
float calculateVelocity(bool left, float speed, float velocity_old)
bool enableMotor(int ax10420)
double get_velocity_left()
double calculateAdapter(double required_velocity, double real_velocity, double adapter)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
int AX10420_SetOutput(int fd, tAXGroup group, tAXPort port, unsigned value)
CanListener * canListener
#define EMERGENCY_STOP_BIT
void setTargetVelocity(const geometry_msgs::Twist &twist)
double get_velocity_right()