blob: 0ca5caad174a4a34e14411a5275a25d70beb880b [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/messages/RobotState.q.h"
8#include "aos/common/control_loop/control_loops.q.h"
9#include "aos/common/logging/logging.h"
10
11#include "frc971/constants.h"
Austin Schuha40624b2013-03-03 14:02:40 -080012#include "frc971/control_loops/wrist/wrist_motor_plant.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080013
14namespace frc971 {
15namespace control_loops {
16
Austin Schuhdc1c84a2013-02-23 16:33:10 -080017WristMotor::WristMotor(control_loops::WristLoop *my_wrist)
18 : aos::control_loops::ControlLoop<control_loops::WristLoop>(my_wrist),
Austin Schuh844960d2013-03-09 17:07:51 -080019 zeroed_joint_(MakeWristLoop()) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080020}
21
Austin Schuh844960d2013-03-09 17:07:51 -080022bool WristMotor::FetchConstants(
23 ZeroedJoint<1>::ConfigurationData *config_data) {
Brian Silverman25362362013-09-22 19:44:25 -070024 if (::aos::robot_state.get() == NULL) {
25 if (!::aos::robot_state.FetchNext()) {
26 LOG(ERROR, "Failed to fetch robot state to get constants.\n");
27 return false;
28 }
29 }
Austin Schuh844960d2013-03-09 17:07:51 -080030 if (!constants::wrist_lower_limit(&config_data->lower_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080031 LOG(ERROR, "Failed to fetch the wrist lower limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080032 return false;
33 }
Austin Schuh844960d2013-03-09 17:07:51 -080034 if (!constants::wrist_upper_limit(&config_data->upper_limit)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080035 LOG(ERROR, "Failed to fetch the wrist upper limit constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080036 return false;
37 }
James Kuszmaule06e2512013-03-02 15:04:53 -080038 if (!constants::wrist_hall_effect_start_angle(
Austin Schuh844960d2013-03-09 17:07:51 -080039 &config_data->hall_effect_start_angle[0])) {
James Kuszmaule06e2512013-03-02 15:04:53 -080040 LOG(ERROR, "Failed to fetch the wrist start angle constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080041 return false;
42 }
Austin Schuhdd3bc412013-03-16 17:02:40 -070043 if (!constants::wrist_zeroing_off_speed(&config_data->zeroing_off_speed)) {
44 LOG(ERROR, "Failed to fetch the wrist zeroing off speed constant.\n");
45 return false;
46 }
47
Austin Schuh844960d2013-03-09 17:07:51 -080048 if (!constants::wrist_zeroing_speed(&config_data->zeroing_speed)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080049 LOG(ERROR, "Failed to fetch the wrist zeroing speed constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080050 return false;
51 }
52
Austin Schuh844960d2013-03-09 17:07:51 -080053 config_data->max_zeroing_voltage = 5.0;
Austin Schuh039d4f92013-04-04 05:52:03 +000054 config_data->deadband_voltage = 0.0;
Austin Schuhfa033692013-02-24 01:00:55 -080055 return true;
56}
57
Austin Schuhfa033692013-02-24 01:00:55 -080058// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080059void WristMotor::RunIteration(
60 const ::aos::control_loops::Goal *goal,
61 const control_loops::WristLoop::Position *position,
62 ::aos::control_loops::Output *output,
Austin Schuh8b43caf2013-03-19 10:04:47 +000063 ::aos::control_loops::Status * status) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080064
Austin Schuhfa033692013-02-24 01:00:55 -080065 // Disable the motors now so that all early returns will return with the
66 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080067 if (output) {
68 output->voltage = 0;
69 }
70
Austin Schuhfa033692013-02-24 01:00:55 -080071 // Cache the constants to avoid error handling down below.
Austin Schuh844960d2013-03-09 17:07:51 -080072 ZeroedJoint<1>::ConfigurationData config_data;
73 if (!FetchConstants(&config_data)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080074 return;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080075 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080076 zeroed_joint_.set_config_data(config_data);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080077 }
78
Austin Schuh844960d2013-03-09 17:07:51 -080079 ZeroedJoint<1>::PositionData transformed_position;
80 ZeroedJoint<1>::PositionData *transformed_position_ptr =
81 &transformed_position;
82 if (!position) {
83 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080084 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080085 transformed_position.position = position->pos;
86 transformed_position.hall_effects[0] = position->hall_effect;
87 transformed_position.hall_effect_positions[0] = position->calibration;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080088 }
89
Austin Schuh844960d2013-03-09 17:07:51 -080090 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
91 output != NULL,
92 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080093
Austin Schuhfa033692013-02-24 01:00:55 -080094 if (position) {
Brian Silverman18f6d872013-03-16 13:55:46 -070095 LOG(DEBUG, "pos: %f hall: %s absolute: %f\n",
96 position->pos,
97 position->hall_effect ? "true" : "false",
98 zeroed_joint_.absolute_position());
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