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