summaryrefslogtreecommitdiff
path: root/digital/io/src/move_cb.c
blob: 7a055e10add833d22cb0baf3c6e5276b5de98677 (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
143
144
/*
 * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT!
 *
 * Skeleton for move callbacks implementation.
 *
 * 
 */
#include "common.h"
#include "fsm.h"
#include "move_cb.h"
#include "move.h"
#include "asserv.h"

/* if the left border is under 500 mm of the actual position do not go there. */
#define MOVE_BORDER_LEVEL 200


/*
 * START =ok=>
 *  => DESIRED_POSITION
 *   Tries to reach a position provided by the user. If the position desired can not be reached, it all try to move on the right or the left.
 */
fsm_branch_t
move__START__ok (void)
{
    asserv_goto (move_data.position_x, move_data.position_y);
    return move_next (START, ok);
}

/*
 * DESIRED_POSITION =failed_or_blocked=>
 * near_left_border => MOVE_ON_RIGHT
 *   Same process as the previous one but on the right.
 * near_right_border => MOVE_ON_LEFT
 *   The robot has failed to reach the position. It shall try another position before trying to reach this one again. It shall go to the on the left only if the left border is the farest one.
 */
fsm_branch_t
move__DESIRED_POSITION__failed_or_blocked (void)
{
    asserv_position_t pos;
    asserv_position_t new_pos;

    asserv_get_position (&pos);
    new_pos = pos;
    new_pos.x += MOVE_BORDER_LEVEL ;

    if (move_can_go_on_left_or_right (pos, new_pos))
      {
	asserv_goto (new_pos.x, new_pos.y);
	return move_next_branch (DESIRED_POSITION, failed_or_blocked, near_left_border);
      }
    else
      {
	new_pos.x = pos.x + MOVE_BORDER_LEVEL;
	// replace this by the correct function.
	asserv_goto (new_pos.x, new_pos.y);
	return move_next_branch (DESIRED_POSITION, failed_or_blocked, near_right_border);
      }
}

/*
 * DESIRED_POSITION =reached=>
 *  => END
 *   The position provided by the user has been reached, the FSM can stop.
 */
fsm_branch_t
move__DESIRED_POSITION__reached (void)
{
    return move_next (DESIRED_POSITION, reached);
}

/*
 * MOVE_ON_RIGHT =failed_or_blocked_or_near_border=>
 *  => MOVE_ON_LEFT
 *   The position is fail again, it will try to reach another one.
 */
fsm_branch_t
move__MOVE_ON_RIGHT__failed_or_blocked_or_near_border (void)
{
    asserv_position_t pos;
    asserv_position_t new_pos;

    asserv_get_position (&pos);
    new_pos.x = pos.x - MOVE_BORDER_LEVEL;
    new_pos.y = pos.y;
    new_pos.a = pos.a;

    if (move_can_go_on_left_or_right (pos, new_pos))
      {
	asserv_goto (new_pos.x, new_pos.y);
      }

    return move_next (MOVE_ON_RIGHT, failed_or_blocked_or_near_border);
}

/*
 * MOVE_ON_RIGHT =reached=>
 *  => DESIRED_POSITION
 *   The position has been reached. It will now try to reach the position provided by the user.
 */
fsm_branch_t
move__MOVE_ON_RIGHT__reached (void)
{
    asserv_goto (move_data.position_x, move_data.position_y);
    return move_next (MOVE_ON_RIGHT, reached);
}

/*
 * MOVE_ON_LEFT =failed_or_blocked_or_near_border=>
 *  => MOVE_ON_RIGHT
 *   The position is fail again, it will try to reach another one.
 */
fsm_branch_t
move__MOVE_ON_LEFT__failed_or_blocked_or_near_border (void)
{
    asserv_position_t pos;
    asserv_position_t new_pos;

    asserv_get_position (&pos);
    new_pos.x = pos.x + MOVE_BORDER_LEVEL;
    new_pos.y = pos.y;
    new_pos.a = pos.a;
    if (move_can_go_on_left_or_right (pos, new_pos))
      {
	// call the correct function to go to the right.
	asserv_goto (new_pos.x, new_pos.y);
      }

    return move_next (MOVE_ON_LEFT, failed_or_blocked_or_near_border);
}

/*
 * MOVE_ON_LEFT =reached=>
 *  => DESIRED_POSITION
 *   The position has been reached. It will now try to reach the position provided by the user.
 */
fsm_branch_t
move__MOVE_ON_LEFT__reached (void)
{
    asserv_goto (move_data.position_x, move_data.position_y);
    return move_next (MOVE_ON_LEFT, reached);
}