Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / station / test_encoders.c @ 1251

History | View | Annotate | Download (2.7 KB)

1
#include "test_encoders.h"
2
#include "global.h"
3
#include "comm_robot.h"
4

    
5
#include "hardware.h"
6

    
7
// TODO as parameters
8
#define num_measurements 4
9
#define velocity 200
10
#define on_delay 500
11
#define off_delay 800
12

    
13
// ******************
14
// ** Data sending **
15
// ******************
16

    
17
static void send_encoder_data (uint8_t num, uint8_t direction, 
18
        int16_t *data_station, int16_t *data_robot)
19
{
20
        if (direction!=motor_direction_off)
21
        {
22
                usb_puts ("data encoder ");
23
                usb_puti (num);
24
                usb_puts (" ");
25
                usb_puts (motor_direction_string (direction));
26
                usb_puts (" ");
27
                        
28
                for (uint8_t i=0; i<num_measurements; ++i)
29
                {
30
                        usb_putc (' ');
31
                        usb_puti (data_station[i]);
32
                        usb_putc ('/');
33
                        usb_puti (data_robot[i]);
34
                }
35

    
36
                usb_puts (NL);
37
        }
38
}
39

    
40

    
41
// ************************
42
// ** Internal functions **
43
// ************************
44

    
45
static void test_encoders_direction (uint8_t direction1, uint8_t direction2)
46
{
47
        // Allocate space for the data on the stack
48
        int16_t *data_station_l  =malloc (num_measurements*sizeof (uint16_t));
49
        int16_t *data_station_r  =malloc (num_measurements*sizeof (uint16_t));
50
        int16_t *data_robot_l    =malloc (num_measurements*sizeof (uint16_t));
51
        int16_t *data_robot_r    =malloc (num_measurements*sizeof (uint16_t));
52

    
53
        usb_puts ("# Resetting encoders" NL);
54
        dynamos_reset ();
55
        robot_reset_encoders ();
56
        
57
        for (uint8_t m=0; m<num_measurements; ++m)
58
        {
59
                robot_set_motors (direction1, velocity, direction2, velocity);
60
                delay_ms (on_delay);
61
                robot_set_motors_off ();
62
                delay_ms (off_delay);
63

    
64
                // Read the station dynamos
65
                dynamos_read (&(data_station_l[m]), &(data_station_r[m]));
66
                
67
                // Read the robot encoders
68
                robot_read_encoders (&(data_robot_l[m]), &(data_robot_r[m]));
69
        }
70

    
71
        send_encoder_data (0, direction1, data_station_l, data_robot_l);
72
        send_encoder_data (1, direction2, data_station_r, data_robot_r);
73
        
74
        free (data_robot_r);
75
        free (data_robot_l);
76
        free (data_station_r);
77
        free (data_station_l);
78
}
79

    
80

    
81
// **********************
82
// ** Public functions **
83
// **********************
84

    
85
void test_encoder_all (void)
86
{
87
        usb_puts("# Testing encoders" NL);
88

    
89
        test_encoders_direction (motor_direction_forward, motor_direction_backward);
90
        test_encoders_direction (motor_direction_backward, motor_direction_forward);
91

    
92
        usb_puts("# Testing encoders finished" NL);
93
}
94

    
95
void test_encoder (uint8_t num)
96
{
97
        if (num==1)
98
        {
99
                test_encoders_direction (motor_direction_forward, motor_direction_off);
100
                test_encoders_direction (motor_direction_backward, motor_direction_off);
101
        }
102
        else if (num==2)
103
        {
104
                test_encoders_direction (motor_direction_off, motor_direction_forward);
105
                test_encoders_direction (motor_direction_off, motor_direction_backward);
106
        }
107
}