vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / projects / microb2010 / ballboard / actuator.c
1 /*  
2  *  Copyright Droids Corporation (2009)
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.2 2009-04-24 19:30:42 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
41 #include <rdline.h>
42
43 #include "actuator.h"
44 #include "main.h"
45
46 #define ROLLER_ON      -ROLLER_SPEED
47 #define ROLLER_OFF     0
48 #define ROLLER_REVERSE ROLLER_SPEED
49
50 #define FORKROT_DEPLOYED -55000
51 #define FORKROT_MID1 -31000
52 #define FORKROT_MID2 -27000
53 #define FORKROT_EJECT   -12000
54 #define FORKROT_PACKED   -4000
55
56 void roller_on(void)
57 {
58         cs_set_consign(&ballboard.roller.cs, ROLLER_ON);
59 }
60
61 void roller_off(void)
62 {
63         cs_set_consign(&ballboard.roller.cs, ROLLER_OFF);
64 }
65
66 void roller_reverse(void)
67 {
68         cs_set_consign(&ballboard.roller.cs, ROLLER_REVERSE);
69 }
70
71 void fork_deploy(void)
72 {
73         cs_set_consign(&ballboard.forkrot.cs, FORKROT_DEPLOYED);
74 }
75
76 void fork_pack(void)
77 {
78         cs_set_consign(&ballboard.forkrot.cs, FORKROT_PACKED);
79 }
80
81 void fork_mid1(void)
82 {
83         cs_set_consign(&ballboard.forkrot.cs, FORKROT_MID1);
84 }
85
86 void fork_mid2(void)
87 {
88         cs_set_consign(&ballboard.forkrot.cs, FORKROT_MID2);
89 }
90
91 void fork_eject(void)
92 {
93         cs_set_consign(&ballboard.forkrot.cs, FORKROT_EJECT);
94 }
95
96 static uint8_t fork_is_at_pos(int32_t pos)
97 {
98         int32_t diff;
99         diff = pos - encoders_spi_get_value(FORKROT_ENCODER);
100         if (diff < 0)
101                 diff = -diff;
102         if (diff < 500)
103                 return 1;
104         return 0;
105 }
106
107 uint8_t fork_is_packed(void)
108 {
109         return fork_is_at_pos(FORKROT_PACKED);
110 }
111
112 void actuator_init(void)
113 {
114         printf_P(PSTR("fork autopos..."));
115         pwm_ng_set(FORKROT_PWM, 400);
116         wait_ms(1000);
117         pwm_ng_set(FORKROT_PWM, 0);
118         encoders_spi_set_value(FORKROT_ENCODER, 0);
119         printf_P(PSTR("ok\r\n"));
120         ballboard.forkrot.on = 1;
121 }