Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #pragma once
00038 #ifndef WIIMOTE_WIIMOTE_CONTROLLER_H
00039 #define WIIMOTE_WIIMOTE_CONTROLLER_H
00040
00041 #include "ros/ros.h"
00042 #include "sensor_msgs/JoyFeedbackArray.h"
00043 #include "std_srvs/Empty.h"
00044 #include "sensor_msgs/Imu.h"
00045
00046 #include "wiimote/stat_vector_3d.h"
00047
00048
00049 #include <bluetooth/bluetooth.h>
00050 namespace wiimote_c
00051 {
00052 #include <cwiid.h>
00053 }
00054
00055 #define zeroedByCal(raw, zero, one) \
00056 (((raw - zero) * 1.0) / ((one - zero) * 1.0))
00057
00058 class WiimoteNode
00059 {
00060 public:
00061 WiimoteNode();
00062 ~WiimoteNode();
00063 char *getBluetoothAddr();
00064 void setBluetoothAddr(const char *bt_str);
00065 bool pairWiimote(int flags, int timeout);
00066 int unpairWiimote();
00067
00068 void publish();
00069
00070 void setLedState(uint8_t led_state);
00071 void setRumbleState(uint8_t rumble);
00072
00073 private:
00074 void setReportMode(uint8_t rpt_mode);
00075 void checkFactoryCalibrationData();
00076 void resetMotionPlusState();
00077 void resetNunchukState();
00078 void resetClassicState();
00079
00080 ros::NodeHandle nh_;
00081
00082 static wiimote_c::cwiid_err_t cwiidErrorCallback;
00083
00100 void joySetFeedbackCallback(const sensor_msgs::JoyFeedbackArray::ConstPtr& feedback);
00101 bool serviceImuCalibrateCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&);
00102
00103 bool isCollectingWiimote();
00104 bool isCollectingNunchuk();
00105 bool isCollectingClassic();
00106 bool isCollectingMotionplus();
00107
00108 bool isPresentNunchuk();
00109 bool isPresentClassic();
00110 bool isPresentMotionplus();
00111
00112 bool calibrateJoystick(uint8_t stick[2], uint8_t (¢er)[2], const char *name);
00113 void updateJoystickMinMax(uint8_t stick[2], uint8_t (&stick_min)[2],
00114 uint8_t (&stick_max)[2], const char *name);
00115 void calculateJoystickAxisXY(uint8_t stick_current[2], uint8_t stick_min[2],
00116 uint8_t stick_max[2], uint8_t stick_center[2], double (&stick)[2]);
00117
00118 void publishJoy();
00119 void publishImuData();
00120 void publishWiimoteState();
00121 bool publishWiimoteNunchukCommon();
00122 void publishWiimoteNunchuk();
00123 void publishWiimoteClassic();
00124
00125 bool getStateSample();
00126
00127 void setLEDBit(uint8_t led, bool on);
00128 void setRumbleBit(bool on);
00129
00130 ros::Publisher joy_pub_;
00131 ros::Publisher imu_data_pub_;
00132 ros::Publisher wiimote_state_pub_;
00133 ros::Publisher wiimote_nunchuk_pub_;
00134 ros::Publisher wiimote_classic_pub_;
00135 ros::Publisher imu_is_calibrated_pub_;
00136
00137 ros::Subscriber joy_set_feedback_sub_;
00138
00139 ros::ServiceServer imu_calibrate_srv_;
00140
00141
00142 bdaddr_t bt_device_addr_;
00143
00144
00145 wiimote_c::cwiid_wiimote_t *wiimote_;
00146
00147
00148 struct wiimote_c::cwiid_state wiimote_state_;
00149 void initializeWiimoteState();
00150
00151 uint32_t state_secs_;
00152 uint32_t state_nsecs_;
00153
00154
00155 uint8_t report_mode_;
00156
00157
00158 struct wiimote_c::acc_cal wiimote_cal_;
00159 bool wiimote_calibrated_;
00160 ros::Time calibration_time_;
00161
00162
00163 const uint8_t JOYSTICK_NUNCHUK_DEFAULT_CENTER_ = 127;
00164 const uint8_t JOYSTICK_NUNCHUK_20PERCENT_MAX_ = 205;
00165 const uint8_t JOYSTICK_NUNCHUK_20PERCENT_MIN_ = 50;
00166 const uint8_t JOYSTICK_CLASSIC_LEFT_DEFAULT_CENTER_ = 31;
00167 const uint8_t JOYSTICK_CLASSIC_LEFT_20PERCENT_MAX_ = 50;
00168 const uint8_t JOYSTICK_CLASSIC_LEFT_20PERCENT_MIN_ = 13;
00169 const uint8_t JOYSTICK_CLASSIC_RIGHT_DEFAULT_CENTER_ = 15;
00170 const uint8_t JOYSTICK_CLASSIC_RIGHT_20PERCENT_MAX_ = 25;
00171 const uint8_t JOYSTICK_CLASSIC_RIGHT_20PERCENT_MIN_ = 6;
00172
00173
00174 struct wiimote_c::acc_cal nunchuk_cal_;
00175 bool nunchuk_calibrated_;
00176 bool nunchuk_failed_calibration_;
00177 uint8_t nunchuk_stick_center_[2];
00178 bool nunchuk_stick_calibrated_;
00179 uint8_t nunchuk_stick_max_[2];
00180 uint8_t nunchuk_stick_min_[2];
00181
00182
00183 uint8_t classic_stick_left_center_[2];
00184 bool classic_stick_left_calibrated_;
00185 uint8_t classic_stick_left_max_[2];
00186 uint8_t classic_stick_left_min_[2];
00187 uint8_t classic_stick_right_center_[2];
00188 bool classic_stick_right_calibrated_;
00189 uint8_t classic_stick_right_max_[2];
00190 uint8_t classic_stick_right_min_[2];
00191
00192 const int IGNORE_DATA_POINTS_ = 100;
00193 const int COVARIANCE_DATA_POINTS_ = 100;
00194 StatVector3d linear_acceleration_stat_;
00195 StatVector3d angular_velocity_stat_;
00196 boost::array<double, 9> linear_acceleration_covariance_;
00197 boost::array<double, 9> angular_velocity_covariance_;
00198
00199 uint8_t led_state_ = 0;
00200 uint8_t rumble_ = 0;
00201
00202 uint64_t wiimote_errors = 0;
00203
00204
00205 const double EARTH_GRAVITY_ = 9.80665;
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 const double GYRO_SCALE_FACTOR_ = 0.001055997;
00229 };
00230
00231 #endif // WIIMOTE_WIIMOTE_CONTROLLER_H