Project

General

Profile

Revision 1418

Added by Rich Hong about 14 years ago

Added encoder_get_x and encoder_get_v.

use get_v at your own risk
If encoder_read returns -1, this usually means battery is low.
If encoder_read returns a value greater than ENCODER_MAX, this usually means there are physical problems with the encoder.

View differences:

trunk/code/projects/test/test_encoders.c
21 21
	usb_init();
22 22
	encoders_init();
23 23
	int encoder_left,encoder_right;
24
	int dx_left, dx_right;
24
	int x_left, x_right;
25
	int v_left, v_right;
25 26
	int tc;
26 27
	while(1) {
27 28
		/* button1 is pressed */
......
40 41
		usb_puti(encoder_right); 
41 42
		usb_puts("\n");
42 43

  
43
		dx_left = encoder_get_dx(LEFT);
44
		dx_right = encoder_get_dx(RIGHT);
44
		x_left = encoder_get_x(LEFT);
45
		x_right = encoder_get_x(RIGHT);
45 46
		usb_puts("Total Distance (left, right): ");
46
		usb_puti(dx_left);
47
		usb_puti(x_left);
47 48
		usb_puts(", ");
48
		usb_puti(dx_right);
49
		usb_puti(x_right);
49 50
		usb_puts("\n");
50 51

  
52
		v_left = encoder_get_v(LEFT);
53
		v_right = encoder_get_v(RIGHT);
54
		usb_puts("Velocity (left, right): ");
55
		usb_puti(v_left);
56
		usb_puts(", ");
57
		usb_puti(v_right);
58
		usb_puts("\n");
59

  
51 60
		tc = encoder_get_tc();
52 61
		usb_puts("Time count: ");
53 62
		usb_puti(tc);
trunk/code/projects/libdragonfly/encoders.c
34 34
inline unsigned int right_data_array_prev(void);
35 35
inline unsigned int right_data_array_bottom(void);
36 36

  
37
//encoder_get_v helper function prototypes
38
int left_data_at(int index);
39
int right_data_at(int index);
40
int get_dx(char encoder, int index);
37 41

  
38 42
void encoder_recv_complete(void){
39 43
  	encoder_buf_index = 0;
......
75 79
 * @param encoder this is the encoder that you want to read. Valid arguments
76 80
 *          are LEFT and RIGHT
77 81
 *
78
 * @return the value of the specified encoder
82
 * @return the value of the specified encoder.
83
 *         -1 usually means low battery.
84
 *         values above ENCODER_MAX usually means phyiscal problems with
85
 *         the encoder.
79 86
 **/
80 87
int encoder_read(char encoder){
81 88
	
......
85 92
}
86 93

  
87 94
/**
95
 * @brief Get total distance travelled by the specified encoder (in encoder ticks)
96
 *
97
 * @param encoder the encoder that you want to read, either LEFT or RIGHT
98
 *
99
 * @return The distance covered by the specified encoder.
100
 *
101
 * @note Simply calls encoder_get_dx.
102
 **/
103
int encoder_get_x(char encoder) {
104
	return encoder_get_dx(encoder);
105
}
106

  
107
/**
88 108
 * Gets the total distance covered by the specified encoder (in encoder clicks)
89 109
 *
90 110
 * @param encoder the encoder that you want to read, use LEFT or RIGHT
......
98 118
	else return -1;
99 119
}
100 120

  
121
/** 
122
 * @brief Returns the approximated instantaneous velocity of the robot
123
 * in terms of encoder clicks.
124
 * 
125
 * @param encoder RIGHT or LEFT - the wheel you want the velocity for.
126
 * 
127
 * @return The instantaneous velocity for the given wheel or twice the ERR_VEL
128
 *         if an error occurs (1024 * 2 = 2048)
129
 *
130
 * @bug This uses hard coded values and results are inconsistent.
131
 *      Use at your own risk.
132
 */
133
int encoder_get_v(char encoder){
134
    int vel1, vel2;
135
    vel1 = get_dx(encoder, 0);
136
    vel2 = get_dx(encoder, 1);
137

  
138
    if (vel1 == ERR_VEL && vel2 == ERR_VEL)
139
        return ERR_VEL << 1;
140
    else if (vel2 == ERR_VEL)
141
        return vel1 << 1;
142
    else if (vel1 == ERR_VEL)
143
        return vel2 << 1;
144
    else
145
        return vel1 + vel2;
146
}
147

  
101 148
/**
102 149
 * Resets the distance accumulator for the specified
103 150
 *  encoder.
......
275 322
		return right_data_array[right_data_idx + 1];
276 323
}
277 324

  
325
/* Helper functions for encoder_get_v */
326
int left_data_at(int index) {
327
    int tmp_idx = left_data_idx - index;
328
    if (tmp_idx < 0)
329
        tmp_idx += BUFFER_SIZE;
330
    return left_data_array[tmp_idx];
331
}
332

  
333
int right_data_at(int index) {
334
    int tmp_idx = right_data_idx - index;
335
    if (tmp_idx < 0)
336
        tmp_idx += BUFFER_SIZE;
337
    return right_data_array[tmp_idx];
338
}
339

  
340
int get_dx(char encoder, int index) {
341
    int dx, ctr;
342
    ctr = 0;
343
    dx = 1024;
344
    do {
345
        if (encoder == LEFT)
346
            dx = left_data_at(index+ctr) - left_data_at(index+ctr+38);
347
        else
348
            dx = right_data_at(index+ctr) - right_data_at(index+ctr+38);
349
        ctr++;
350
    } while ((dx > 30 || dx < -30) && ctr < 3); 
351
    if (dx > 30 || dx < -30)
352
        return ERR_VEL;
353
    return dx; 
354
}
trunk/code/projects/libdragonfly/encoders.h
28 28
	#define RIGHT 1
29 29
#endif
30 30

  
31
/** @brief Max value of valid encoder reading. **/
32
#define ENCODER_MAX 1024
33

  
31 34
/** @brief Magnet misaligned - likely distance from encoder problem. **/
32 35
#define ENCODER_MAGNET_FAILURE 1025
33 36
/** @brief Encoder misaligned - likely on XY plane. **/
......
49 52
#define MagDECn _BV(0)
50 53

  
51 54
/** @brief Buffer size **/
52
#define BUFFER_SIZE 23
55
#define BUFFER_SIZE 46
53 56

  
57
#define ERR_VEL 1024
58

  
54 59
/** @brief Initialize encoders. **/
55 60
void encoders_init(void);
56 61
/** @brief Read instantaneous encoder value. **/
57 62
int encoder_read(char encoder);
58 63

  
64
/** @brief Get total distance traveled.
65
 *  @note  Simply calls encoder_get_dx.
66
 **/
67
int encoder_get_x(char encoder);
68

  
69
/** @brief Get instantaneous velocity. **/
70
int encoder_get_v(char encoder);
71

  
59 72
/** @brief Get total distance traveled. **/
60 73
int encoder_get_dx(char encoder);
61 74
/** @brief Reset distance counter. **/

Also available in: Unified diff