4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief Test for PWM driver (implementation)
35 * This is a simple test for PWM driver. This module
36 * is target independent, so you can test all target that
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
42 * Afther this, all is ready and you can test PWM driver.
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.
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.
57 * \author Daniele Basile <asterix@develer.com>
59 * \brief HW test for DC Motor.
62 #include <cfg/cfg_dc_motor.h>
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
71 #include <algo/pid_control.h>
73 #include <drv/timer.h>
74 #include <drv/dc_motor.h>
77 #include <drv/dc_motor.h>
79 #include <kern/proc.h>
86 static DCMotorConfig motor =
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 */
99 .pid_enable = true, /* Enable or disable pid control */
102 .pwm_dev = 2, /* PWM channel */
103 .freq = 3000, /* Frquency of PWM output waveform */
106 .adc_ch = 2, /* ADC channel */
107 .adc_max = 65535, /* Max range value for ADC */
108 .adc_min = 0, /* Min range value for ADC */
111 .dir = 1, /* Default spin direction of DC motor */
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 */
118 int dc_motor_testSetUp(void)
124 #if !CFG_PWM_ENABLE_OLD_API
134 void NORETURN dc_motor_testRun(void)
139 * Assign the configuration to motor.
141 dc_motor_setup(MOTOR, &motor);
146 * Using enable and disable
148 dc_motor_setDir(MOTOR, 1);
149 dc_motor_setSpeed(MOTOR, 10000);
150 dc_motor_enable(MOTOR, true);
152 dc_motor_enable(MOTOR, false);
155 dc_motor_setDir(MOTOR, 0);
156 dc_motor_setSpeed(MOTOR, 60000);
157 dc_motor_enable(MOTOR, true);
159 dc_motor_enable(MOTOR, false);
164 dc_motor_setDir(MOTOR, 1);
165 dc_motor_setSpeed(MOTOR, 60000);
166 dc_motor_startTimer(MOTOR, 150);
167 dc_motor_waitStop(MOTOR);
169 dc_motor_setDir(MOTOR, 0);
170 dc_motor_setSpeed(MOTOR, 10000);
171 dc_motor_startTimer(MOTOR, 500);
172 dc_motor_waitStop(MOTOR);
177 int dc_motor_testTearDown(void)