vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / projects / microb2010 / cobboard / shovel.c
1 /*
2  *  Copyright Droids Corporation (2010)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: actuator.c,v 1.4 2009-04-24 19:30:41 zer0 Exp $
19  *
20  */
21
22 #include <aversive.h>
23 #include <aversive/pgmspace.h>
24 #include <aversive/wait.h>
25 #include <aversive/error.h>
26
27 #include <ax12.h>
28 #include <uart.h>
29 #include <spi.h>
30 #include <encoders_spi.h>
31 #include <pwm_ng.h>
32 #include <timer.h>
33 #include <scheduler.h>
34 #include <clock_time.h>
35
36 #include <pid.h>
37 #include <quadramp.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
40 #include <rdline.h>
41
42 #include "main.h"
43 #include "state.h"
44 #include "shovel.h"
45
46 #define SHOVEL_DOWN 100
47 #define SHOVEL_MID  4500
48 #define SHOVEL_UP   11300
49 #define SHOVEL_KICKSTAND_UP   13400
50 #define SHOVEL_KICKSTAND_DOWN 10400
51
52 static int32_t shovel_k1 = 1000;
53 static int32_t shovel_k2 = 20;
54 static uint8_t shovel_current_limit = 1;
55
56 /* init spickle position at beginning */
57 static void shovel_autopos(void)
58 {
59         printf_P(PSTR("shovel autopos..."));
60         pwm_ng_set(SHOVEL_PWM, -500);
61         wait_ms(1000);
62         pwm_ng_set(LEFT_SPICKLE_PWM, 0);
63         encoders_spi_set_value(SHOVEL_ENCODER, 0);
64         printf_P(PSTR("ok\r\n"));
65 }
66
67 static uint8_t shovel_is_at_pos(int32_t pos)
68 {
69         int32_t diff;
70         diff = pos - encoders_spi_get_value(SHOVEL_ENCODER);
71         if (diff < 0)
72                 diff = -diff;
73         if (diff < 500)
74                 return 1;
75         return 0;
76 }
77
78 void shovel_set_current_limit_coefs(int32_t k1, int32_t k2)
79 {
80         shovel_k1 = k1;
81         shovel_k2 = k2;
82 }
83
84 uint8_t shovel_get_current_limit_coefs(int32_t *k1, int32_t *k2)
85 {
86         *k1 = shovel_k1;
87         *k2 = shovel_k2;
88         return shovel_current_limit;
89 }
90
91 void shovel_current_limit_enable(uint8_t enable)
92 {
93         shovel_current_limit = enable;
94 }
95
96 /* Set CS command for shovel. Called by CS manager. */
97 void shovel_set(void *mot, int32_t cmd)
98 {
99         static int32_t oldpos;
100         int32_t pos, maxcmd, speed;
101
102         pos = encoders_spi_get_value(SHOVEL_ENCODER);
103         if (shovel_current_limit) {
104                 speed = pos - oldpos;
105                 if (speed > 0 && cmd < 0)
106                         maxcmd = shovel_k1;
107                 else if (speed < 0 && cmd > 0)
108                         maxcmd = shovel_k1;
109                 else {
110                         speed = ABS(speed);
111                         maxcmd = shovel_k1 + shovel_k2 * speed;
112                 }
113                 if (cmd > maxcmd)
114                         cmd = maxcmd;
115                 else if (cmd < -maxcmd)
116                         cmd = -maxcmd;
117         }
118
119         pwm_ng_set(mot, cmd);
120         oldpos = pos;
121 }
122
123 void shovel_down(void)
124 {
125         shovel_current_limit_enable(0);
126         quadramp_set_1st_order_vars(&cobboard.shovel.qr, 2500, 2500);
127         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 50, 80);
128         cs_set_consign(&cobboard.shovel.cs, SHOVEL_DOWN);
129 }
130
131 void shovel_mid(void)
132 {
133         shovel_current_limit_enable(0);
134         quadramp_set_1st_order_vars(&cobboard.shovel.qr, 2500, 2500);
135         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 80, 80);
136         cs_set_consign(&cobboard.shovel.cs, SHOVEL_MID);
137 }
138
139 void shovel_up(void)
140 {
141         shovel_current_limit_enable(0);
142         if (state_get_cob_count() <= 1)
143                 quadramp_set_1st_order_vars(&cobboard.shovel.qr, 700, 2500);
144         else
145                 quadramp_set_1st_order_vars(&cobboard.shovel.qr, 1000, 2500);
146         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 80, 15);
147         cs_set_consign(&cobboard.shovel.cs, SHOVEL_UP);
148 }
149
150 void shovel_kickstand_up(void)
151 {
152         shovel_set_current_limit_coefs(1000, 20);
153         shovel_current_limit_enable(1);
154         quadramp_set_1st_order_vars(&cobboard.shovel.qr, 200, 200);
155         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 10, 10);
156         cs_set_consign(&cobboard.shovel.cs, SHOVEL_KICKSTAND_UP);
157 }
158
159 void shovel_kickstand_down(void)
160 {
161         shovel_set_current_limit_coefs(500, 0);
162         shovel_current_limit_enable(1);
163         quadramp_set_1st_order_vars(&cobboard.shovel.qr, 200, 200);
164         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 10, 10);
165         cs_set_consign(&cobboard.shovel.cs, SHOVEL_KICKSTAND_DOWN);
166 }
167
168 uint8_t shovel_is_up(void)
169 {
170         return shovel_is_at_pos(SHOVEL_UP);
171 }
172
173 uint8_t shovel_is_down(void)
174 {
175         return shovel_is_at_pos(SHOVEL_DOWN);
176 }
177
178 uint8_t shovel_is_mid(void)
179 {
180         return shovel_is_at_pos(SHOVEL_MID);
181 }
182
183 void shovel_init(void)
184 {
185         shovel_autopos();
186         cobboard.shovel.on = 1;
187 }