Austin Schuh | a40624b | 2013-03-03 14:02:40 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/wrist/wrist.h" |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 7 | #include "aos/common/messages/RobotState.q.h" |
| 8 | #include "aos/common/control_loop/control_loops.q.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | |
| 11 | #include "frc971/constants.h" |
Austin Schuh | a40624b | 2013-03-03 14:02:40 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/wrist/wrist_motor_plant.h" |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 13 | |
| 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 17 | WristMotor::WristMotor(control_loops::WristLoop *my_wrist) |
| 18 | : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist), |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 19 | zeroed_joint_(MakeWristLoop()) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 20 | } |
| 21 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 22 | bool WristMotor::FetchConstants( |
| 23 | ZeroedJoint<1>::ConfigurationData *config_data) { |
Brian Silverman | 2536236 | 2013-09-22 19:44:25 -0700 | [diff] [blame] | 24 | if (::aos::robot_state.get() == NULL) { |
| 25 | if (!::aos::robot_state.FetchNext()) { |
| 26 | LOG(ERROR, "Failed to fetch robot state to get constants.\n"); |
| 27 | return false; |
| 28 | } |
| 29 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 30 | if (!constants::wrist_lower_limit(&config_data->lower_limit)) { |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 31 | LOG(ERROR, "Failed to fetch the wrist lower limit constant.\n"); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 32 | return false; |
| 33 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 34 | if (!constants::wrist_upper_limit(&config_data->upper_limit)) { |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 35 | LOG(ERROR, "Failed to fetch the wrist upper limit constant.\n"); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 36 | return false; |
| 37 | } |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 38 | if (!constants::wrist_hall_effect_start_angle( |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 39 | &config_data->hall_effect_start_angle[0])) { |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 40 | LOG(ERROR, "Failed to fetch the wrist start angle constant.\n"); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 41 | return false; |
| 42 | } |
Austin Schuh | dd3bc41 | 2013-03-16 17:02:40 -0700 | [diff] [blame] | 43 | if (!constants::wrist_zeroing_off_speed(&config_data->zeroing_off_speed)) { |
| 44 | LOG(ERROR, "Failed to fetch the wrist zeroing off speed constant.\n"); |
| 45 | return false; |
| 46 | } |
| 47 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 48 | if (!constants::wrist_zeroing_speed(&config_data->zeroing_speed)) { |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 49 | LOG(ERROR, "Failed to fetch the wrist zeroing speed constant.\n"); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 53 | config_data->max_zeroing_voltage = 5.0; |
Austin Schuh | 039d4f9 | 2013-04-04 05:52:03 +0000 | [diff] [blame] | 54 | config_data->deadband_voltage = 0.0; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 58 | // Positive angle is up, and positive power is up. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 59 | void WristMotor::RunIteration( |
| 60 | const ::aos::control_loops::Goal *goal, |
| 61 | const control_loops::WristLoop::Position *position, |
| 62 | ::aos::control_loops::Output *output, |
Austin Schuh | 8b43caf | 2013-03-19 10:04:47 +0000 | [diff] [blame] | 63 | ::aos::control_loops::Status * status) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 64 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 65 | // Disable the motors now so that all early returns will return with the |
| 66 | // motors disabled. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 67 | if (output) { |
| 68 | output->voltage = 0; |
| 69 | } |
| 70 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 71 | // Cache the constants to avoid error handling down below. |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 72 | ZeroedJoint<1>::ConfigurationData config_data; |
| 73 | if (!FetchConstants(&config_data)) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 74 | return; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 75 | } else { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 76 | zeroed_joint_.set_config_data(config_data); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 79 | ZeroedJoint<1>::PositionData transformed_position; |
| 80 | ZeroedJoint<1>::PositionData *transformed_position_ptr = |
| 81 | &transformed_position; |
| 82 | if (!position) { |
| 83 | transformed_position_ptr = NULL; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 84 | } else { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 85 | transformed_position.position = position->pos; |
| 86 | transformed_position.hall_effects[0] = position->hall_effect; |
| 87 | transformed_position.hall_effect_positions[0] = position->calibration; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 90 | const double voltage = zeroed_joint_.Update(transformed_position_ptr, |
| 91 | output != NULL, |
| 92 | goal->goal, 0.0); |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 93 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 94 | if (position) { |
Brian Silverman | 18f6d87 | 2013-03-16 13:55:46 -0700 | [diff] [blame] | 95 | LOG(DEBUG, "pos: %f hall: %s absolute: %f\n", |
| 96 | position->pos, |
| 97 | position->hall_effect ? "true" : "false", |
| 98 | zeroed_joint_.absolute_position()); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 101 | if (output) { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 102 | output->voltage = voltage; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 103 | } |
Austin Schuh | 8b43caf | 2013-03-19 10:04:47 +0000 | [diff] [blame] | 104 | status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace control_loops |
| 108 | } // namespace frc971 |