blob: 3c76ff83e84288abefcfa001cdab5b5798b748c2 [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"
Brian Silvermanc5277542013-03-22 13:33:07 -070012#include "aos/atom_code/messages/DriverStationDisplay.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080013
14#include "frc971/constants.h"
Austin Schuha40624b2013-03-03 14:02:40 -080015#include "frc971/control_loops/wrist/wrist_motor_plant.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080016
17namespace frc971 {
18namespace control_loops {
19
Austin Schuhdc1c84a2013-02-23 16:33:10 -080020WristMotor::WristMotor(control_loops::WristLoop *my_wrist)
21 : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist),
Austin Schuh844960d2013-03-09 17:07:51 -080022 zeroed_joint_(MakeWristLoop()) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080023}
24
Austin Schuh844960d2013-03-09 17:07:51 -080025bool WristMotor::FetchConstants(
26 ZeroedJoint<1>::ConfigurationData *config_data) {
27 if (!constants::wrist_lower_limit(&config_data->lower_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080028 LOG(ERROR, "Failed to fetch the wrist lower limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080029 return false;
30 }
Austin Schuh844960d2013-03-09 17:07:51 -080031 if (!constants::wrist_upper_limit(&config_data->upper_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080032 LOG(ERROR, "Failed to fetch the wrist upper limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080033 return false;
34 }
James Kuszmaule06e2512013-03-02 15:04:53 -080035 if (!constants::wrist_hall_effect_start_angle(
Austin Schuh844960d2013-03-09 17:07:51 -080036 &config_data->hall_effect_start_angle[0])) {
James Kuszmaule06e2512013-03-02 15:04:53 -080037 LOG(ERROR, "Failed to fetch the wrist start angle constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080038 return false;
39 }
Austin Schuhdd3bc412013-03-16 17:02:40 -070040 if (!constants::wrist_zeroing_off_speed(&config_data->zeroing_off_speed)) {
41 LOG(ERROR, "Failed to fetch the wrist zeroing off speed constant.\n");
42 return false;
43 }
44
Austin Schuh844960d2013-03-09 17:07:51 -080045 if (!constants::wrist_zeroing_speed(&config_data->zeroing_speed)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080046 LOG(ERROR, "Failed to fetch the wrist zeroing speed constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080047 return false;
48 }
49
Austin Schuh844960d2013-03-09 17:07:51 -080050 config_data->max_zeroing_voltage = 5.0;
Austin Schuh039d4f92013-04-04 05:52:03 +000051 config_data->deadband_voltage = 0.0;
Austin Schuhfa033692013-02-24 01:00:55 -080052 return true;
53}
54
Austin Schuhfa033692013-02-24 01:00:55 -080055// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080056void WristMotor::RunIteration(
57 const ::aos::control_loops::Goal *goal,
58 const control_loops::WristLoop::Position *position,
59 ::aos::control_loops::Output *output,
Austin Schuh8b43caf2013-03-19 10:04:47 +000060 ::aos::control_loops::Status * status) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080061
Austin Schuhfa033692013-02-24 01:00:55 -080062 // Disable the motors now so that all early returns will return with the
63 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080064 if (output) {
65 output->voltage = 0;
66 }
67
Austin Schuhfa033692013-02-24 01:00:55 -080068 // Cache the constants to avoid error handling down below.
Austin Schuh844960d2013-03-09 17:07:51 -080069 ZeroedJoint<1>::ConfigurationData config_data;
70 if (!FetchConstants(&config_data)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080071 return;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080072 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080073 zeroed_joint_.set_config_data(config_data);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080074 }
75
Austin Schuh844960d2013-03-09 17:07:51 -080076 ZeroedJoint<1>::PositionData transformed_position;
77 ZeroedJoint<1>::PositionData *transformed_position_ptr =
78 &transformed_position;
79 if (!position) {
80 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080081 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080082 transformed_position.position = position->pos;
83 transformed_position.hall_effects[0] = position->hall_effect;
84 transformed_position.hall_effect_positions[0] = position->calibration;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080085 }
86
Austin Schuh844960d2013-03-09 17:07:51 -080087 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
88 output != NULL,
89 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080090
Austin Schuhfa033692013-02-24 01:00:55 -080091 if (position) {
Brian Silverman18f6d872013-03-16 13:55:46 -070092 LOG(DEBUG, "pos: %f hall: %s absolute: %f\n",
93 position->pos,
94 position->hall_effect ? "true" : "false",
95 zeroed_joint_.absolute_position());
Brian Silvermanc5277542013-03-22 13:33:07 -070096
97 ::aos::DriverStationDisplay::Send(3, "wrist: %6.4f ",
98 position->calibration);
Austin Schuhfa033692013-02-24 01:00:55 -080099 }
100
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800101 if (output) {
Austin Schuh844960d2013-03-09 17:07:51 -0800102 output->voltage = voltage;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800103 }
Austin Schuh8b43caf2013-03-19 10:04:47 +0000104 status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800105}
106
107} // namespace control_loops
108} // namespace frc971