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/control_loop/control_loops.q.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | |
| 10 | #include "frc971/constants.h" |
Austin Schuh | a40624b | 2013-03-03 14:02:40 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/wrist/wrist_motor_plant.h" |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace control_loops { |
| 15 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 16 | WristMotor::WristMotor(control_loops::WristLoop *my_wrist) |
| 17 | : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist), |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 18 | zeroed_joint_(MakeWristLoop()) { |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 19 | { |
| 20 | using ::frc971::constants::GetValues; |
| 21 | ZeroedJoint<1>::ConfigurationData config_data; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 22 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 23 | config_data.lower_limit = GetValues().wrist_lower_limit; |
| 24 | config_data.upper_limit = GetValues().wrist_upper_limit; |
| 25 | config_data.hall_effect_start_angle[0] = |
| 26 | GetValues().wrist_hall_effect_start_angle; |
| 27 | config_data.zeroing_off_speed = GetValues().wrist_zeroing_off_speed; |
| 28 | config_data.zeroing_speed = GetValues().wrist_zeroing_speed; |
Austin Schuh | dd3bc41 | 2013-03-16 17:02:40 -0700 | [diff] [blame] | 29 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 30 | config_data.max_zeroing_voltage = 5.0; |
| 31 | config_data.deadband_voltage = 0.0; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 32 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 33 | zeroed_joint_.set_config_data(config_data); |
| 34 | } |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 37 | // Positive angle is up, and positive power is up. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 38 | void WristMotor::RunIteration( |
| 39 | const ::aos::control_loops::Goal *goal, |
| 40 | const control_loops::WristLoop::Position *position, |
| 41 | ::aos::control_loops::Output *output, |
Austin Schuh | 8b43caf | 2013-03-19 10:04:47 +0000 | [diff] [blame] | 42 | ::aos::control_loops::Status * status) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 43 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 44 | // Disable the motors now so that all early returns will return with the |
| 45 | // motors disabled. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 46 | if (output) { |
| 47 | output->voltage = 0; |
| 48 | } |
| 49 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 50 | ZeroedJoint<1>::PositionData transformed_position; |
| 51 | ZeroedJoint<1>::PositionData *transformed_position_ptr = |
| 52 | &transformed_position; |
| 53 | if (!position) { |
| 54 | transformed_position_ptr = NULL; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 55 | } else { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 56 | transformed_position.position = position->pos; |
| 57 | transformed_position.hall_effects[0] = position->hall_effect; |
| 58 | transformed_position.hall_effect_positions[0] = position->calibration; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 61 | const double voltage = zeroed_joint_.Update(transformed_position_ptr, |
| 62 | output != NULL, |
| 63 | goal->goal, 0.0); |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 64 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 65 | if (position) { |
Brian Silverman | 18f6d87 | 2013-03-16 13:55:46 -0700 | [diff] [blame] | 66 | LOG(DEBUG, "pos: %f hall: %s absolute: %f\n", |
| 67 | position->pos, |
| 68 | position->hall_effect ? "true" : "false", |
| 69 | zeroed_joint_.absolute_position()); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 72 | if (output) { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 73 | output->voltage = voltage; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 74 | } |
Austin Schuh | 8b43caf | 2013-03-19 10:04:47 +0000 | [diff] [blame] | 75 | status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | } // namespace control_loops |
| 79 | } // namespace frc971 |