summaryrefslogtreecommitdiff
path: root/digital/mimot/src/dirty/state.h
blob: 4c1a28ed298b1ff1d7c54d8d0a442b525ad37b4f (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
#ifndef state_h
#define state_h
/* state.h */
/* asserv - Position & speed motor control on AVR. {{{
 *
 * Copyright (C) 2008 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

/** There is two mechanism to acknowledge long lived command completion.
 *
 * The first one is dedicated to unreliable and full duplex channels like the
 * serial port.  It is based on sequence numbers.
 *
 * The second one is simpler, but can only be used with reliable and half
 * duplex channels like the TWI bus.  It just use a flag to remember command
 * completion witch is reseted when a new command arrives. */

/** Control mode. */
enum state_mode_t
{
    /** Simple PWM setup mode. */
    MODE_PWM,
    /** Position control mode. */
    MODE_POS,
    /** Speed control mode. */
    MODE_SPEED,
    /** Trajectory control mode. */
    MODE_TRAJ,
};

/** Current motor state. */
struct state_t
{
    /** Current motor control mode. */
    uint8_t mode;
    /** Control mode variant.
     * Used for main motors:
     *  - bit 0: disable theta position control.
     *  - bit 1: disable alpha position control.
     *  - bit 2: disable blocking detection. */
    uint8_t variant;
    /** Sequence number of the currently processed command, should be between
     * 1 and 127.  When a command is received on the serial port it is ignored
     * if its sequence number is equal to the current sequence number.  In
     * this case a duplicated message is inferred. */
    uint8_t sequence;
    /** Sequence number of the most recently finished command.  When a command
     * is finished, the current sequence number is copied to this variable. */
    uint8_t sequence_finish;
    /** Sequence number of the most recently acknowledged command.  Until this
     * is not equal to the last finished command sequence number, a message is
     * generated on the serial line. */
    uint8_t sequence_ack;
    /** Simpler flag based mechanism, indicates if the last received command
     * is finished. */
    uint8_t finished;
    /** Wether the motor is blocked. */
    uint8_t blocked;
};

/** Main motors state. */
extern struct state_t state_main;

/** Start a new command execution. */
static inline void
state_start (struct state_t *motor, uint8_t mode, uint8_t sequence)
{
    motor->mode = mode;
    motor->variant = 0;
    motor->sequence = sequence;
    motor->finished = 0;
    motor->blocked = 0;
}

/** Signal the current command completion. */
static inline void
state_finish (struct state_t *motor)
{
    motor->sequence_finish = motor->sequence;
    motor->finished = 1;
}

/** Signal the current command is blocked, disable motor control until a new
 * command is given. */
static inline void
state_blocked (struct state_t *motor)
{
    motor->sequence_finish = motor->sequence | 0x80;
    motor->blocked = 1;
    motor->mode = MODE_PWM;
}

/** Acknowledge a command completion and blocked state. */
static inline void
state_acknowledge (struct state_t *motor, uint8_t sequence)
{
    motor->sequence_ack = sequence;
    if (sequence == motor->sequence_finish)
      {
	motor->finished = 0;
	motor->blocked = 0;
      }
}

#endif /* state_h */