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