Update preset.
[bertos.git] / bertos / drv / dc_motor_hwtest.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test for PWM driver (implementation)
34  *
35  * This is a simple test for PWM driver. This module
36  * is target independent, so you can test all target that
37  * BeRTOS support.
38  * To use this test you should include a pwm_map.h header where
39  * are defined the PWM channels for your target. Then you should add
40  * or remove a test setting in pwm_test_cfg array, and edit a value for
41  * your specific test.
42  * Afther this, all is ready and you can test PWM driver.
43  *
44  * The test check first if all PWM channel starts, and then try
45  * to change a PWM duty cicle for all channel.
46  * The change of duty cycle is operate when a PWM channel is enable,
47  * in this way you can see if a pwm signal is clean and work properly.
48  * The duty value is change incrementaly, and when it arrive to 100% or 0%,
49  * we reset the duty value and restart the test.
50  * Further the duty test, we check also a PWM polarity, infact when we
51  * reach a reset duty value, we invert a polary of PWM wavform.
52  * So you can see if the hardware manage correctly this situation.
53  *
54  * Note: To be simple and target independently we not use a timer module,
55  * and so the delay is do with a for cycle.
56  *
57  * \author Daniele Basile <asterix@develer.com>
58  *
59  * \brief HW test for DC Motor.
60  */
61
62 #include <cfg/cfg_dc_motor.h>
63
64 #include <cfg/debug.h>
65 // Define logging setting (for cfg/log.h module).
66 #define LOG_LEVEL         DC_MOTOR_LOG_LEVEL
67 #define LOG_VERBOSITY     DC_MOTOR_LOG_FORMAT
68 #include <cfg/log.h>
69
70
71 #include <algo/pid_control.h>
72
73 #include <drv/timer.h>
74 #include <drv/dc_motor.h>
75 #include <drv/adc.h>
76 #include <drv/pwm.h>
77 #include <drv/dc_motor.h>
78
79 #include <kern/proc.h>
80
81 #include <cpu/irq.h>
82
83 #include <verstag.h>
84 #include <buildrev.h>
85
86 static DCMotorConfig motor =
87 {
88     /* PID */
89     {
90         .kp = 1,           /* Proportional coefficient */
91         .ki = 4,           /* Integral coefficient */
92         .kd = 0.008,         /* Derivate coefficient */
93         .i_max = 2E33,       /* Integrale max error value */
94         .i_min = -2E33,      /* Integrale min error value */
95         .out_max = 65535,    /* Max output value */
96         .out_min = 0,        /* Min output value */
97         .sample_period = 0  /* Millisecod between 2 output singal sampling */
98     },
99     .pid_enable = true,      /* Enable or disable pid control */
100
101     /* PWM */
102     .pwm_dev = 2,            /* PWM channel */
103     .freq = 3000,            /* Frquency of PWM output waveform */
104
105     /* ADC */
106     .adc_ch = 2,             /* ADC channel */
107     .adc_max = 65535,        /* Max range value for ADC */
108     .adc_min = 0,            /* Min range value for ADC */
109
110         /* DC Motor */
111     .dir = 1,                 /* Default spin direction of DC motor */
112         .braked = true,
113
114     .speed = 10000,          /* Fixed speed value for seldc_motor_enableect DC motor, if enable_dev_speed flag is false */
115     .speed_dev_id = 7,        /* Index of the device where read speed */
116 };
117
118 int dc_motor_testSetUp(void)
119 {
120         IRQ_ENABLE;
121         kdbg_init();
122         timer_init();
123         proc_init();
124 #if !CFG_PWM_ENABLE_OLD_API
125         pwm_init();
126 #endif
127         adc_init();
128
129         return 0;
130 }
131
132 #define MOTOR      2
133
134 void NORETURN dc_motor_testRun(void)
135 {
136         dc_motor_init();
137
138         /*
139          * Assign the configuration to motor.
140          */
141         dc_motor_setup(MOTOR, &motor);
142
143         while (1)
144         {
145                 /*
146                  * Using enable and disable
147                  */
148                 dc_motor_setDir(MOTOR, 1);
149                 dc_motor_setSpeed(MOTOR, 10000);
150                 dc_motor_enable(MOTOR, true);
151                 timer_delay(500);
152                 dc_motor_enable(MOTOR, false);
153
154
155                 dc_motor_setDir(MOTOR, 0);
156                 dc_motor_setSpeed(MOTOR, 60000);
157                 dc_motor_enable(MOTOR, true);
158                 timer_delay(150);
159                 dc_motor_enable(MOTOR, false);
160
161                 /*
162                  * Using timer
163                  */
164                 dc_motor_setDir(MOTOR, 1);
165                 dc_motor_setSpeed(MOTOR, 60000);
166                 dc_motor_startTimer(MOTOR, 150);
167                 dc_motor_waitStop(MOTOR);
168
169                 dc_motor_setDir(MOTOR, 0);
170                 dc_motor_setSpeed(MOTOR, 10000);
171                 dc_motor_startTimer(MOTOR, 500);
172                 dc_motor_waitStop(MOTOR);
173         }
174
175 }
176
177 int dc_motor_testTearDown(void)
178 {
179         return 0;
180 }