better movements
[aversive.git] / projects / microb2009 / mechboard / 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.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 <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 "../common/i2c_commands.h"
44 #include "actuator.h"
45 #include "ax12_user.h"
46 #include "main.h"
47
48 #define FINGER_DEBUG(args...) DEBUG(E_USER_FINGER, args)
49 #define FINGER_NOTICE(args...) NOTICE(E_USER_FINGER, args)
50 #define FINGER_ERROR(args...) ERROR(E_USER_FINGER, args)
51
52 struct finger {
53         int8_t event;
54         uint16_t destination;
55 };
56
57 struct finger finger;
58
59 static void finger_goto_cb(void *);
60
61 /* schedule a single event for the finger */
62 static void finger_schedule_event(struct finger *finger)
63 {
64         uint8_t flags;
65         int8_t ret;
66
67         IRQ_LOCK(flags);
68         ret = scheduler_add_event(SCHEDULER_SINGLE,
69                                   (void *)finger_goto_cb,
70                                   finger, 1, ARM_PRIO);
71         if (ret == -1) {
72                 IRQ_UNLOCK(flags);
73                 FINGER_ERROR("Cannot load finger event");
74                 return;
75         }
76         finger->event = ret;
77         IRQ_UNLOCK(flags);
78 }
79
80 static void finger_goto_cb(void *data)
81 {
82         uint8_t flags;
83         struct finger *finger = data;
84         uint16_t position;
85
86         IRQ_LOCK(flags);
87         finger->event = -1;
88         position = finger->destination;
89         IRQ_UNLOCK(flags);
90         FINGER_DEBUG("goto_cb %d", position);
91         ax12_user_write_int(&gen.ax12, FINGER_AX12,
92                             AA_GOAL_POSITION_L, position);
93 }
94
95 /* load an event that will move the ax12 for us */
96 void finger_goto(uint16_t position)
97 {
98         uint8_t flags;
99         FINGER_NOTICE("goto %d", position);
100
101         IRQ_LOCK(flags);
102         finger.destination = position;
103         if (finger.event != -1) {
104                 IRQ_UNLOCK(flags);
105                 return; /* nothing to do, event already scheduled */
106         }
107         IRQ_UNLOCK(flags);
108         finger_schedule_event(&finger);
109 }
110
111 static void finger_init(void)
112 {
113         finger.event = -1;
114         finger.destination = 0;
115         /* XXX set pos ? */
116 }
117
118 uint16_t finger_get_goal_pos(void)
119 {
120         return finger.destination;
121 }
122
123 uint8_t finger_get_side(void)
124 {
125         if (finger.destination <= FINGER_CENTER)
126                 return I2C_LEFT_SIDE;
127         return I2C_RIGHT_SIDE;
128 }
129
130 /**********/
131
132 #define SERVO_LEFT_OUT  400
133 #define SERVO_LEFT_1LIN 520
134 #define SERVO_LEFT_2LIN 485
135 #define SERVO_RIGHT_OUT  290
136 #define SERVO_RIGHT_1LIN 155
137 #define SERVO_RIGHT_2LIN 180
138
139 void servo_lintel_out(void)
140 {
141         mechboard.servo_lintel_left = SERVO_LEFT_OUT;
142         mechboard.servo_lintel_right = SERVO_RIGHT_OUT;
143 }
144
145 void servo_lintel_1lin(void)
146 {
147         mechboard.servo_lintel_left = SERVO_LEFT_1LIN;
148         mechboard.servo_lintel_right = SERVO_RIGHT_1LIN;
149 }
150
151 void servo_lintel_2lin(void)
152 {
153         mechboard.servo_lintel_left = SERVO_LEFT_2LIN;
154         mechboard.servo_lintel_right = SERVO_RIGHT_2LIN;
155 }
156
157 /**********/
158
159 void actuator_init(void)
160 {
161         finger_init();
162         servo_lintel_out();
163 }