blob: 392e630edbde75fe2576a5a0f563f508c1f9bd0e [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
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 Schuha40624b2013-03-03 14:02:40 -080014#include "frc971/control_loops/wrist/wrist_motor_plant.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080015
16namespace frc971 {
17namespace control_loops {
18
Austin Schuhdc1c84a2013-02-23 16:33:10 -080019WristMotor::WristMotor(control_loops::WristLoop *my_wrist)
20 : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist),
Austin Schuh844960d2013-03-09 17:07:51 -080021 zeroed_joint_(MakeWristLoop()) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080022}
23
Austin Schuh844960d2013-03-09 17:07:51 -080024bool WristMotor::FetchConstants(
25 ZeroedJoint<1>::ConfigurationData *config_data) {
26 if (!constants::wrist_lower_limit(&config_data->lower_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080027 LOG(ERROR, "Failed to fetch the wrist lower limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080028 return false;
29 }
Austin Schuh844960d2013-03-09 17:07:51 -080030 if (!constants::wrist_upper_limit(&config_data->upper_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080031 LOG(ERROR, "Failed to fetch the wrist upper limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080032 return false;
33 }
James Kuszmaule06e2512013-03-02 15:04:53 -080034 if (!constants::wrist_hall_effect_start_angle(
Austin Schuh844960d2013-03-09 17:07:51 -080035 &config_data->hall_effect_start_angle[0])) {
James Kuszmaule06e2512013-03-02 15:04:53 -080036 LOG(ERROR, "Failed to fetch the wrist start angle constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080037 return false;
38 }
Austin Schuhdd3bc412013-03-16 17:02:40 -070039 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 Schuh844960d2013-03-09 17:07:51 -080044 if (!constants::wrist_zeroing_speed(&config_data->zeroing_speed)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080045 LOG(ERROR, "Failed to fetch the wrist zeroing speed constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080046 return false;
47 }
48
Austin Schuh844960d2013-03-09 17:07:51 -080049 config_data->max_zeroing_voltage = 5.0;
Austin Schuhfa033692013-02-24 01:00:55 -080050 return true;
51}
52
Austin Schuhfa033692013-02-24 01:00:55 -080053// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080054void WristMotor::RunIteration(
55 const ::aos::control_loops::Goal *goal,
56 const control_loops::WristLoop::Position *position,
57 ::aos::control_loops::Output *output,
Austin Schuh8b43caf2013-03-19 10:04:47 +000058 ::aos::control_loops::Status * status) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080059
Austin Schuhfa033692013-02-24 01:00:55 -080060 // Disable the motors now so that all early returns will return with the
61 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080062 if (output) {
63 output->voltage = 0;
64 }
65
Austin Schuhfa033692013-02-24 01:00:55 -080066 // Cache the constants to avoid error handling down below.
Austin Schuh844960d2013-03-09 17:07:51 -080067 ZeroedJoint<1>::ConfigurationData config_data;
68 if (!FetchConstants(&config_data)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080069 return;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080070 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080071 zeroed_joint_.set_config_data(config_data);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080072 }
73
Austin Schuh844960d2013-03-09 17:07:51 -080074 ZeroedJoint<1>::PositionData transformed_position;
75 ZeroedJoint<1>::PositionData *transformed_position_ptr =
76 &transformed_position;
77 if (!position) {
78 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080079 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080080 transformed_position.position = position->pos;
81 transformed_position.hall_effects[0] = position->hall_effect;
82 transformed_position.hall_effect_positions[0] = position->calibration;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080083 }
84
Austin Schuh844960d2013-03-09 17:07:51 -080085 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
86 output != NULL,
87 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080088
Austin Schuhfa033692013-02-24 01:00:55 -080089 if (position) {
Brian Silverman18f6d872013-03-16 13:55:46 -070090 LOG(DEBUG, "pos: %f hall: %s absolute: %f\n",
91 position->pos,
92 position->hall_effect ? "true" : "false",
93 zeroed_joint_.absolute_position());
Austin Schuhfa033692013-02-24 01:00:55 -080094 }
95
Austin Schuhdc1c84a2013-02-23 16:33:10 -080096 if (output) {
Austin Schuh844960d2013-03-09 17:07:51 -080097 output->voltage = voltage;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080098 }
Austin Schuh8b43caf2013-03-19 10:04:47 +000099 status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800100}
101
102} // namespace control_loops
103} // namespace frc971