00001
00002
00003
00004 #ifndef NMC_PIC_SERVO_MSG_BUILDER_H
00005 #define NMC_PIC_SERVO_MSG_BUILDER_H
00006
00007
00008 #include "nmc_msg_builder.H"
00009
00010
00011 namespace nmc
00012 {
00013 namespace pic_servo
00014 {
00015 enum status_report_t
00016 {
00017 STATUS_REPORT_POSITION = 0x01,
00018 STATUS_REPORT_AD_VALUE = 0x02,
00019 STATUS_REPORT_ACTUAL_VELOCITY = 0x04,
00020 STATUS_REPORT_AUXILIARY_STATUS = 0x08,
00021 STATUS_REPORT_HOME_POSITION = 0x10,
00022 STATUS_REPORT_DEVICE_TYPE = 0x20,
00023 };
00024
00025 class reset_position_builder_t : public msg_builder_t
00026 {
00027 protected:
00028 u8_t _build (void);
00029 };
00030
00031 class define_status_builder_t : public msg_builder_t
00032 {
00033 public:
00034 define_status_builder_t () : m_report (0) {}
00035
00036 void report (status_report_t status)
00037 {
00038 m_report &= ~((u8_t) status);
00039 m_report |= (u8_t) status;
00040 }
00041
00042 protected:
00043 u8_t _build (void);
00044
00045 u8_t m_report;
00046 };
00047
00048 class read_status_builder_t : public msg_builder_t
00049 {
00050 public:
00051 read_status_builder_t () : m_report (0) {}
00052
00053 void report (status_report_t status)
00054 {
00055 m_report &= ~((u8_t) status);
00056 m_report |= (u8_t) status;
00057 }
00058
00059 protected:
00060 u8_t _build (void);
00061
00062 u8_t m_report;
00063 };
00064
00065 class load_trajectory_builder_t : public msg_builder_t
00066 {
00067 public:
00068 load_trajectory_builder_t () : m_control (0) {}
00069
00070
00071 void trapezoidal_profile (s32_t pos)
00072 {
00073 m_control = 0x11;
00074 m_position = pos;
00075 }
00076 void trapezoidal_profile_max_velocity (u32_t vel)
00077 {
00078 m_control |= 0x02;
00079 m_velocity = vel;
00080 }
00081 void trapezoidal_profile_acceleration (u32_t acc)
00082 {
00083 m_control |= 0x04;
00084 m_acceleration = acc;
00085 }
00086
00087
00088 void velocity_profile (u32_t vel)
00089 {
00090 m_control = 0x32;
00091 m_velocity = vel;
00092 }
00093 void velocity_profile_reverse (void)
00094 { m_control |= 0x40; }
00095 void velocity_profile_acceleration (u32_t acc)
00096 {
00097 m_control |= 0x04;
00098 m_acceleration = acc;
00099 }
00100
00101
00102 void pwm_value (u8_t pwm)
00103 {
00104 m_control = 0x04;
00105 m_pwm = pwm;
00106 }
00107 void pwm_reverse (void) { m_control |= 0x40; }
00108
00109 void execute_now (void) { m_control |= 0x80; }
00110
00111 protected:
00112 u8_t m_control;
00113 s32_t m_position;
00114 u32_t m_velocity;
00115 u32_t m_acceleration;
00116 u8_t m_pwm;
00117
00118 u8_t _build (void);
00119 };
00120
00121 class start_motion_builder_t : public msg_builder_t
00122 {
00123 protected:
00124 u8_t _build (void);
00125 };
00126
00127 class set_gain_builder_t : public msg_builder_t
00128 {
00129 public:
00130 set_gain_builder_t () :
00131 m_position_gain_kp (0),
00132 m_velocity_gain_kd (0),
00133 m_position_gain_ki (0),
00134 m_integration_limit_il (0),
00135 m_output_limit_ol (0),
00136 m_current_limit_cl (0),
00137 m_position_error_limit_el (0),
00138 m_servo_rate_divisor_sr (0),
00139 m_amplifier_deadband_compensation (0)
00140 {}
00141
00142 void position_gain_kp (u16_t gain)
00143 { m_position_gain_kp = gain; }
00144 void velocity_gain_kd (u16_t gain)
00145 { m_velocity_gain_kd = gain; }
00146 void position_gain_ki (u16_t gain)
00147 { m_position_gain_ki = gain; }
00148 void integration_limit_il (u16_t limit)
00149 { m_integration_limit_il = limit; }
00150 void output_limit_ol (u8_t limit)
00151 { m_output_limit_ol = limit; }
00152 void current_limit_cl (u8_t limit)
00153 { m_current_limit_cl = limit; }
00154 void position_error_limit_el (u16_t limit)
00155 { m_position_error_limit_el = limit; }
00156 void servo_rate_divisor_sr (u8_t divisor)
00157 { m_servo_rate_divisor_sr = divisor; }
00158 void amplifier_deadband_compensation (u8_t band)
00159 { m_amplifier_deadband_compensation = band; }
00160
00161 protected:
00162 u8_t _build (void);
00163
00164 u16_t m_position_gain_kp;
00165 u16_t m_velocity_gain_kd;
00166 u16_t m_position_gain_ki;
00167 u16_t m_integration_limit_il;
00168 u8_t m_output_limit_ol;
00169 u8_t m_current_limit_cl;
00170 u16_t m_position_error_limit_el;
00171 u8_t m_servo_rate_divisor_sr;
00172 u8_t m_amplifier_deadband_compensation;
00173 };
00174
00175 class stop_motor_builder_t : public msg_builder_t
00176 {
00177 public:
00178 stop_motor_builder_t () :
00179 m_mode (MODE_TURN_OFF) {}
00180
00181 void amplifier_enable (void)
00182 { m_mode = MODE_AMPLIFIER_ENABLE; }
00183 void turn_off (void)
00184 { m_mode = MODE_TURN_OFF; }
00185 void stop_abruptly (void)
00186 { m_mode = MODE_STOP_ABRUPTLY; }
00187 void stop_smoothly (void)
00188 { m_mode = MODE_STOP_SMOOTHLY; }
00189 void stop_here (s32_t position)
00190 {
00191 m_mode = MODE_STOP_HERE;
00192 m_position = position;
00193 }
00194
00195 protected:
00196 enum mode_t
00197 {
00198 MODE_AMPLIFIER_ENABLE = 0x01,
00199 MODE_TURN_OFF = 0x02,
00200 MODE_STOP_ABRUPTLY = 0x04,
00201 MODE_STOP_SMOOTHLY = 0x08,
00202 MODE_STOP_HERE = 0x10,
00203 };
00204
00205 u8_t _build (void);
00206
00207 mode_t m_mode;
00208 s32_t m_position;
00209 };
00210
00211 class io_control_builder_t : public msg_builder_t
00212 {
00213 public:
00214 io_control_builder_t () :
00215 m_limit_1_output (false),
00216 m_limit_2_output (false),
00217 m_limit_1_is_input (true),
00218 m_limit_2_is_input (true)
00219 {
00220 }
00221
00222 protected:
00223 u8_t _build (void);
00224
00225 bool m_limit_1_output;
00226 bool m_limit_2_output;
00227 bool m_limit_1_is_input;
00228 bool m_limit_2_is_input;
00229 };
00230
00231 class set_homing_mode_builder_t : public msg_builder_t
00232 {
00233 public:
00234 set_homing_mode_builder_t () :
00235 m_conditions (0),
00236 m_stop_mode (SEEK_STOP_TURN_OFF) {}
00237
00238 enum seek_condition_t
00239 {
00240 SEEK_CONDITION_LIMIT_1_CHANGE = 0x01,
00241 SEEK_CONDITION_LIMIT_2_CHANGE = 0x02,
00242 SEEK_CONDITION_INDEX_CHANGE = 0x08,
00243 SEEK_CONDITION_POSITION_ERROR = 0x40,
00244 SEEK_CONDITION_CURRENT_LIMIT = 0x80,
00245 };
00246
00247 void condition_enable (seek_condition_t condition)
00248 { m_conditions |= (u8_t) condition; }
00249
00250 enum seek_stop_mode_t
00251 {
00252 SEEK_STOP_TURN_OFF = 0x04,
00253 SEEK_STOP_ABRUPTLY = 0x10,
00254 SEEK_STOP_SMOOTHLY = 0x20,
00255 };
00256
00257 void stop_mode (seek_stop_mode_t mode)
00258 { m_stop_mode = mode; }
00259
00260 protected:
00261 u8_t _build (void);
00262
00263 u8_t m_conditions;
00264 seek_stop_mode_t m_stop_mode;
00265 };
00266
00267 class clear_sticky_bits_builder_t : public msg_builder_t
00268 {
00269 protected:
00270 u8_t _build (void);
00271 };
00272
00273 class save_as_home_builder_t : public msg_builder_t
00274 {
00275 protected:
00276 u8_t _build (void);
00277 };
00278 };
00279 };
00280
00281
00282 #endif