blob: 8229c930432041a87bb4fe27e2e4878f5b49419c [file] [log] [blame]
Austin Schuha40624b2013-03-03 14:02:40 -08001#include "frc971/control_loops/wrist/wrist.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -08002
3#include <stdio.h>
4
5#include <algorithm>
6
Austin Schuhdc1c84a2013-02-23 16:33:10 -08007#include "aos/common/control_loop/control_loops.q.h"
8#include "aos/common/logging/logging.h"
9
10#include "frc971/constants.h"
Austin Schuha40624b2013-03-03 14:02:40 -080011#include "frc971/control_loops/wrist/wrist_motor_plant.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080012
13namespace frc971 {
14namespace control_loops {
15
Austin Schuhdc1c84a2013-02-23 16:33:10 -080016WristMotor::WristMotor(control_loops::WristLoop *my_wrist)
17 : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist),
Austin Schuh844960d2013-03-09 17:07:51 -080018 zeroed_joint_(MakeWristLoop()) {
Brian Silverman431500a2013-10-28 19:50:15 -070019 {
20 using ::frc971::constants::GetValues;
21 ZeroedJoint<1>::ConfigurationData config_data;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080022
Brian Silverman431500a2013-10-28 19:50:15 -070023 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 Schuhdd3bc412013-03-16 17:02:40 -070029
Brian Silverman431500a2013-10-28 19:50:15 -070030 config_data.max_zeroing_voltage = 5.0;
31 config_data.deadband_voltage = 0.0;
Austin Schuhfa033692013-02-24 01:00:55 -080032
Brian Silverman431500a2013-10-28 19:50:15 -070033 zeroed_joint_.set_config_data(config_data);
34 }
Austin Schuhfa033692013-02-24 01:00:55 -080035}
36
Austin Schuhfa033692013-02-24 01:00:55 -080037// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080038void WristMotor::RunIteration(
39 const ::aos::control_loops::Goal *goal,
40 const control_loops::WristLoop::Position *position,
41 ::aos::control_loops::Output *output,
Austin Schuh8b43caf2013-03-19 10:04:47 +000042 ::aos::control_loops::Status * status) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080043
Austin Schuhfa033692013-02-24 01:00:55 -080044 // Disable the motors now so that all early returns will return with the
45 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080046 if (output) {
47 output->voltage = 0;
48 }
49
Austin Schuh844960d2013-03-09 17:07:51 -080050 ZeroedJoint<1>::PositionData transformed_position;
51 ZeroedJoint<1>::PositionData *transformed_position_ptr =
52 &transformed_position;
53 if (!position) {
54 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080055 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080056 transformed_position.position = position->pos;
57 transformed_position.hall_effects[0] = position->hall_effect;
58 transformed_position.hall_effect_positions[0] = position->calibration;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080059 }
60
Austin Schuh844960d2013-03-09 17:07:51 -080061 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
62 output != NULL,
63 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080064
Austin Schuhfa033692013-02-24 01:00:55 -080065 if (position) {
Brian Silverman18f6d872013-03-16 13:55:46 -070066 LOG(DEBUG, "pos: %f hall: %s absolute: %f\n",
67 position->pos,
68 position->hall_effect ? "true" : "false",
69 zeroed_joint_.absolute_position());
Austin Schuhfa033692013-02-24 01:00:55 -080070 }
71
Austin Schuhdc1c84a2013-02-23 16:33:10 -080072 if (output) {
Austin Schuh844960d2013-03-09 17:07:51 -080073 output->voltage = voltage;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080074 }
Austin Schuh8b43caf2013-03-19 10:04:47 +000075 status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080076}
77
78} // namespace control_loops
79} // namespace frc971