cobboard: better actions + i2c
[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 "shovel.h"
44
45 #define SHOVEL_DOWN 100
46 #define SHOVEL_MID  4900
47 #define SHOVEL_UP   10000
48
49 /* init spickle position at beginning */
50 static void shovel_autopos(void)
51 {
52         printf_P(PSTR("shovel autopos..."));
53         pwm_ng_set(SHOVEL_PWM, -500);
54         wait_ms(1000);
55         pwm_ng_set(LEFT_SPICKLE_PWM, 0);
56         encoders_spi_set_value(SHOVEL_ENCODER, 0);
57         printf_P(PSTR("ok\r\n"));
58 }
59
60 static uint8_t shovel_is_at_pos(int32_t pos)
61 {
62         int32_t diff;
63         diff = pos - encoders_spi_get_value(SHOVEL_ENCODER);
64         if (diff < 0)
65                 diff = -diff;
66         if (diff < 500)
67                 return 1;
68         return 0;
69 }
70
71 void shovel_down(void)
72 {
73         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 50, 80);
74         cs_set_consign(&cobboard.shovel.cs, SHOVEL_DOWN);
75 }
76
77 void shovel_mid(void)
78 {
79         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 80, 80);
80         cs_set_consign(&cobboard.shovel.cs, SHOVEL_MID);
81 }
82
83 void shovel_up(void)
84 {
85         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 80, 20);
86         cs_set_consign(&cobboard.shovel.cs, SHOVEL_UP);
87 }
88
89 uint8_t shovel_is_up(void)
90 {
91         return shovel_is_at_pos(SHOVEL_UP);
92 }
93
94 uint8_t shovel_is_down(void)
95 {
96         return shovel_is_at_pos(SHOVEL_DOWN);
97 }
98
99 void shovel_init(void)
100 {
101         shovel_autopos();
102         cobboard.shovel.on = 1;
103 }