summaryrefslogtreecommitdiff
path: root/digital/avr/modules/motor/control_system/control_system.c
blob: 96f0cb58085e094ca28e20ce2d9e03d71552fd5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* control_system.c */
/* motor - Motor control module. {{{
 *
 * Copyright (C) 2011 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "control_system.h"

void
control_system_single_init (control_system_single_t *cs)
{
    speed_control_init (&cs->speed, &cs->pos);
    pos_control_init (&cs->pos);
}

void
control_system_polar_init (control_system_polar_t *cs)
{
    speed_control_init (&cs->speed_theta, &cs->pos_theta);
    speed_control_init (&cs->speed_alpha, &cs->pos_alpha);
    pos_control_init (&cs->pos_theta);
    pos_control_init (&cs->pos_alpha);
}

void
control_system_single_update_prepare (control_system_single_t *cs)
{
    uint8_t control_modes = cs->state.modes;
    /* Update speed control. */
    speed_control_update (&cs->speed, control_modes & CS_MODE_SPEED_CONTROL);
}

void
control_system_polar_update_prepare (control_system_polar_t *cs)
{
    uint8_t control_modes = cs->state.modes;
    /* Update speed control. */
    speed_control_update (&cs->speed_theta,
			  control_modes & CS_MODE_SPEED_CONTROL);
    speed_control_update (&cs->speed_alpha,
			  control_modes & CS_MODE_SPEED_CONTROL);
}

void
control_system_single_update (control_system_single_t *cs)
{
    uint8_t control_modes = cs->state.modes;
    int16_t current_speed;
    int16_t out;
    /* Update pos control. */
    current_speed = cs->encoder->diff;
    out = pos_control_update (&cs->pos, current_speed,
			      control_modes & CS_MODE_POS_CONTROL);
    /* Test for blocking condition, detection is done if control is enabled,
     * but action is taken depending on the blocking detection mode. */
    blocking_detection_update (&cs->blocking_detection, current_speed, out,
			       &cs->pos, control_modes & CS_MODE_POS_CONTROL);
    if (control_modes & CS_MODE_BLOCKING_DETECTION
	&& cs->blocking_detection.blocked)
      {
	control_state_blocked (&cs->state);
	/* Reset control so that another action can be done immediately. */
	pos_control_update (&cs->pos, 0, 0);
	blocking_detection_update (&cs->blocking_detection, 0, 0,
				   &cs->pos, 0);
      }
    /* Set output value if under position control, zero on blocking. */
    if (control_modes & CS_MODE_POS_CONTROL)
	output_set (cs->output, out);
    else if (control_modes & CS_MODE_BLOCKED)
	output_set (cs->output, 0);
}

void
control_system_polar_update (control_system_polar_t *cs)
{
    uint8_t control_modes = cs->state.modes;
    int16_t current_speed_theta, current_speed_alpha;
    int16_t out_theta, out_alpha;
    /* Update pos control. */
    current_speed_theta = cs->encoder_right->diff + cs->encoder_left->diff;
    current_speed_alpha = cs->encoder_right->diff - cs->encoder_left->diff;
    out_theta = pos_control_update
	(&cs->pos_theta, current_speed_theta,
	 control_modes & CS_MODE_POS_CONTROL_THETA);
    out_alpha = pos_control_update
	(&cs->pos_alpha, current_speed_alpha,
	 control_modes & CS_MODE_POS_CONTROL_ALPHA);
    /* Test for blocking condition, detection is done if control is enabled,
     * but action is taken depending on the blocking detection mode. */
    blocking_detection_update (&cs->blocking_detection_theta,
			       current_speed_theta, out_theta, &cs->pos_theta,
			       control_modes & CS_MODE_POS_CONTROL_THETA);
    blocking_detection_update (&cs->blocking_detection_alpha,
			       current_speed_alpha, out_alpha, &cs->pos_alpha,
			       control_modes & CS_MODE_POS_CONTROL_ALPHA);
    if (control_modes & CS_MODE_BLOCKING_DETECTION
	&& (cs->blocking_detection_theta.blocked
	    || cs->blocking_detection_alpha.blocked))
      {
	control_state_blocked (&cs->state);
	/* Reset control so that another action can be done immediately. */
	pos_control_update (&cs->pos_theta, 0, 0);
	pos_control_update (&cs->pos_alpha, 0, 0);
	blocking_detection_update (&cs->blocking_detection_theta, 0, 0,
				   &cs->pos_theta, 0);
	blocking_detection_update (&cs->blocking_detection_alpha, 0, 0,
				   &cs->pos_alpha, 0);
      }
    /* Set output value if under position control, zero on blocking. */
    if (control_modes & (CS_MODE_POS_CONTROL_THETA |
			 CS_MODE_POS_CONTROL_ALPHA))
      {
	output_set (cs->output_left, out_theta - out_alpha);
	output_set (cs->output_right, out_theta + out_alpha);
      }
    else if (control_modes & CS_MODE_BLOCKED)
      {
	output_set (cs->output_left, 0);
	output_set (cs->output_right, 0);
      }
}