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