blob: 97a0cef27fe8ab0063f71843709c461b4ebb5b57 [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 Schuhfa033692013-02-24 01:00:55 -080051 return true;
52}
53
Austin Schuhfa033692013-02-24 01:00:55 -080054// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080055void WristMotor::RunIteration(
56 const ::aos::control_loops::Goal *goal,
57 const control_loops::WristLoop::Position *position,
58 ::aos::control_loops::Output *output,
Austin Schuh8b43caf2013-03-19 10:04:47 +000059 ::aos::control_loops::Status * status) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080060
Austin Schuhfa033692013-02-24 01:00:55 -080061 // Disable the motors now so that all early returns will return with the
62 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080063 if (output) {
64 output->voltage = 0;
65 }
66
Austin Schuhfa033692013-02-24 01:00:55 -080067 // Cache the constants to avoid error handling down below.
Austin Schuh844960d2013-03-09 17:07:51 -080068 ZeroedJoint<1>::ConfigurationData config_data;
69 if (!FetchConstants(&config_data)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080070 return;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080071 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080072 zeroed_joint_.set_config_data(config_data);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080073 }
74
Austin Schuh844960d2013-03-09 17:07:51 -080075 ZeroedJoint<1>::PositionData transformed_position;
76 ZeroedJoint<1>::PositionData *transformed_position_ptr =
77 &transformed_position;
78 if (!position) {
79 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080080 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080081 transformed_position.position = position->pos;
82 transformed_position.hall_effects[0] = position->hall_effect;
83 transformed_position.hall_effect_positions[0] = position->calibration;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080084 }
85
Austin Schuh844960d2013-03-09 17:07:51 -080086 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
87 output != NULL,
88 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080089
Austin Schuhfa033692013-02-24 01:00:55 -080090 if (position) {
Brian Silverman18f6d872013-03-16 13:55:46 -070091 LOG(DEBUG, "pos: %f hall: %s absolute: %f\n",
92 position->pos,
93 position->hall_effect ? "true" : "false",
94 zeroed_joint_.absolute_position());
Brian Silvermanc5277542013-03-22 13:33:07 -070095
96 ::aos::DriverStationDisplay::Send(3, "wrist: %6.4f ",
97 position->calibration);
Austin Schuhfa033692013-02-24 01:00:55 -080098 }
99
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800100 if (output) {
Austin Schuh844960d2013-03-09 17:07:51 -0800101 output->voltage = voltage;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800102 }
Austin Schuh8b43caf2013-03-19 10:04:47 +0000103 status->done = ::std::abs(zeroed_joint_.absolute_position() - goal->goal) < 0.004;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800104}
105
106} // namespace control_loops
107} // namespace frc971