Project

General

Profile

Revision 1206

Fixed code duplication

View differences:

trunk/code/projects/diagnostic_station/station/test_motors.c
24 24
	//	}
25 25
}
26 26

  
27
static void test_motors_direction (uint8_t direction1, uint8_t direction2)
27

  
28
#define acceleration_increasing 1
29
#define acceleration_decreasing 2
30

  
31
static void test_motors_direction_acceleration (uint8_t direction1, uint8_t direction2, uint8_t vel_start, uint8_t vel_end, int8_t vel_step)
28 32
{
29 33
	// Use 16 bit variable for vel to avoid problems with overflow (if vel=250 and 10 is added, it would be 260-256=4
30 34
	// which is still smaller than vel_max). There are more elegant solutions to this problem, but this one is
31 35
	// easy and performance is not an issue here anyway.
36

  
37
	// Distinguish between start>=end/start<end because we have to use the corresponding stop condition.
38
	if (vel_end>=vel_start)
39
		for (uint16_t vel=vel_start; vel<=vel_end; vel+=vel_step)
40
			test_motors_direction_velocity (direction1, direction2, vel);
41
	else
42
		for (uint16_t vel=vel_start; vel>=vel_end; vel+=vel_step)
43
			test_motors_direction_velocity (direction1, direction2, vel);
32 44
	
33
	for (uint16_t vel=vel_min; vel<=vel_max; vel+=vel_inc)
34
		test_motors_direction_velocity (direction1, direction2, vel);
35

  
45
 	// Send data
36 46
	for (uint8_t i=1; i<=2; ++i)
37 47
	{
......
44 54
			usb_puti (i);
45 55
			usb_puts (" ");
46 56
			usb_puts (motor_direction_string (direction));
47
			usb_puts (" increasing ");
57
			
58
			if (vel_end>vel_start)
59
				usb_puts (" increasing ");
60
			else if (vel_end<vel_start)
61
				usb_puts (" decreasing ");
62
			else
63
				usb_puts (" constant ");
64
				
48 65
			usb_puts (" 160/100 170/200 180/300 190/400 200/500 210/600 220/700" NL);
49 66
		}
50 67
	}
68
}
51 69

  
52
	// TODO code duplication
53
	
54
	for (uint16_t vel=vel_max; vel>=vel_min; vel-=vel_inc)
55
		test_motors_direction_velocity (direction1, direction2, vel);
56
		
57
	// Send data
58
	for (uint8_t i=1; i<=2; ++i)
59
	{
60
		uint8_t direction=(i==1)?direction1:direction2;
61

  
62
		if (direction!=motor_direction_off)
63
		{
64
			usb_puts ("data motor ");
65
			usb_puti (i);
66
			usb_puts (" ");
67
			usb_puts (motor_direction_string (direction));
68
			usb_puts (" decreasing ");
69
			usb_puts (" 220/700 210/600 200/500 190/400 180/300 170/200 160/100" NL);
70
		}
71
	}
72
	
73

  
70
static void test_motors_direction (uint8_t direction1, uint8_t direction2)
71
{
72
	test_motors_direction_acceleration (direction1, direction2, vel_min, vel_max, vel_inc);
73
	test_motors_direction_acceleration (direction1, direction2, vel_max, vel_min, -vel_inc);
74 74
}
75 75

  
76 76

  

Also available in: Unified diff