blob: 85cc2216ea6898e6a42600d8f968a9ea8f34b06d [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 Schuh844960d2013-03-09 17:07:51 -080039 if (!constants::wrist_zeroing_speed(&config_data->zeroing_speed)) {
James Kuszmaule06e2512013-03-02 15:04:53 -080040 LOG(ERROR, "Failed to fetch the wrist zeroing speed constant.\n");
Austin Schuhfa033692013-02-24 01:00:55 -080041 return false;
42 }
43
Austin Schuh844960d2013-03-09 17:07:51 -080044 config_data->max_zeroing_voltage = 5.0;
Austin Schuhfa033692013-02-24 01:00:55 -080045 return true;
46}
47
Austin Schuhfa033692013-02-24 01:00:55 -080048// Positive angle is up, and positive power is up.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080049void WristMotor::RunIteration(
50 const ::aos::control_loops::Goal *goal,
51 const control_loops::WristLoop::Position *position,
52 ::aos::control_loops::Output *output,
53 ::aos::control_loops::Status * /*status*/) {
54
Austin Schuhfa033692013-02-24 01:00:55 -080055 // Disable the motors now so that all early returns will return with the
56 // motors disabled.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080057 if (output) {
58 output->voltage = 0;
59 }
60
Austin Schuhfa033692013-02-24 01:00:55 -080061 // Cache the constants to avoid error handling down below.
Austin Schuh844960d2013-03-09 17:07:51 -080062 ZeroedJoint<1>::ConfigurationData config_data;
63 if (!FetchConstants(&config_data)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080064 return;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080065 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080066 zeroed_joint_.set_config_data(config_data);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080067 }
68
Austin Schuh844960d2013-03-09 17:07:51 -080069 ZeroedJoint<1>::PositionData transformed_position;
70 ZeroedJoint<1>::PositionData *transformed_position_ptr =
71 &transformed_position;
72 if (!position) {
73 transformed_position_ptr = NULL;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080074 } else {
Austin Schuh844960d2013-03-09 17:07:51 -080075 transformed_position.position = position->pos;
76 transformed_position.hall_effects[0] = position->hall_effect;
77 transformed_position.hall_effect_positions[0] = position->calibration;
78 printf("Position is %f\n", position->pos);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080079 }
80
Austin Schuh844960d2013-03-09 17:07:51 -080081 const double voltage = zeroed_joint_.Update(transformed_position_ptr,
82 output != NULL,
83 goal->goal, 0.0);
Austin Schuh06ee48e2013-03-02 01:47:54 -080084
Austin Schuhfa033692013-02-24 01:00:55 -080085 if (position) {
Austin Schuh844960d2013-03-09 17:07:51 -080086 //LOG(DEBUG, "pos=%f zero=%f currently %f hall: %s\n",
87 //position->pos, zero_offset_, absolute_position,
88 //position->hall_effect ? "true" : "false");
Austin Schuhfa033692013-02-24 01:00:55 -080089 }
90
Austin Schuhdc1c84a2013-02-23 16:33:10 -080091 if (output) {
Austin Schuh844960d2013-03-09 17:07:51 -080092 output->voltage = voltage;
93 printf("Voltage applied is %f\n", voltage);
Austin Schuhdc1c84a2013-02-23 16:33:10 -080094 }
95}
96
97} // namespace control_loops
98} // namespace frc971