00001 //****************************************************************************** 00011 //****************************************************************************** 00012 00013 /* Copyright (c) 2008, Atmel Corporation All rights reserved. 00014 * 00015 * Redistribution and use in source and binary forms, with or without 00016 * modification, are permitted provided that the following conditions are met: 00017 * 00018 * 1. Redistributions of source code must retain the above copyright notice, 00019 * this list of conditions and the following disclaimer. 00020 * 00021 * 2. Redistributions in binary form must reproduce the above copyright notice, 00022 * this list of conditions and the following disclaimer in the documentation 00023 * and/or other materials provided with the distribution. 00024 * 00025 * 3. The name of ATMEL may not be used to endorse or promote products derived 00026 * from this software without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED 00029 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00030 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND 00031 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00032 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00033 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00035 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00036 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00037 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 //_____ I N C L U D E S ___________________________________________________ 00042 #include "config.h" 00043 00044 #include "mc_interface.h" 00045 #include "mc_control.h" 00046 #include "mc_drv.h" 00047 00048 Bool mci_direction = CW; 00049 Bool mci_run_stop = FALSE; 00050 U8 mci_ref_speed = 0; 00051 00052 00053 U16 mci_measured_period = (U16)K_SPEED; 00054 U32 mci_measured_current = 0; 00055 U8 mc_potentiometer_value = 0; 00056 00057 extern Bool overcurrent; 00058 00059 extern U8 new_position; 00060 extern U8 ru_step_length; 00061 extern U8 ru_period_counter; 00062 extern U8 ramp_up_index; 00063 extern U8 motor_state; 00064 00070 void mci_run(void) 00071 { 00072 if (!(PCTL & (1<<PRUN))) /* if there is an overcurrent */ 00073 { 00074 PSC_Init(); 00075 } 00076 overcurrent = FALSE; 00077 mci_run_stop = TRUE; 00078 mc_regulation_loop(); 00079 mc_disable_during_inrush(); /* disable overcurrent during inrush */ 00080 new_position = MS_001; 00081 mc_switch_commutation((Motor_Position)new_position); 00082 mc_duty_cycle(ALIGN_DUTY); 00083 ru_period_counter = ALIGN_TIME; 00084 motor_state=MS_ALIGN; 00085 ru_step_length = RAMP_UP_STEP_MAX; 00086 ramp_up_index = 0; 00087 } 00088 00094 void mci_retry_run(void) 00095 { 00096 mci_run_stop = TRUE; 00097 mc_regulation_loop(); 00098 mc_switch_commutation(mc_get_hall()); 00099 mc_disable_during_inrush(); /* disable overcurrent during inrush */ 00100 } 00101 00107 Bool mci_motor_is_running(void) 00108 { 00109 return mci_run_stop; 00110 } 00111 00118 void mci_stop(void) 00119 { 00120 mci_run_stop=FALSE; 00121 motor_state=MS_STOP; 00122 mc_duty_cycle(0); // Motor Voltage = 0 00123 } 00124 00129 void mc_init() 00130 { 00131 mc_init_HW(); 00132 Enable_interrupt(); 00133 mci_stop(); 00134 mci_forward(); 00135 mci_set_ref_speed(0); 00136 mc_set_measured_period(K_SPEED); 00137 } 00138 00139 /* 00140 * @brief speed modification 00141 * @pre initialization of motor 00142 * @post new value of speed 00143 */ 00144 void mci_set_ref_speed(U8 speed) 00145 { 00146 mci_ref_speed = speed; 00147 } 00148 00149 00150 00151 00152 /* 00153 * @brief speed visualization 00154 * @pre initialization of motor 00155 * @post get speed value 00156 */ 00157 U8 mci_get_ref_speed(void) 00158 { 00159 return mci_ref_speed; 00160 } 00161 00162 /* 00163 * @brief direction modification 00164 * @pre initialization of motor 00165 * @post new value of direction 00166 */ 00167 void mci_forward(void) 00168 { 00169 mci_direction = CW; 00170 } 00171 00172 /* 00173 * @brief direction modification 00174 * @pre initialization of motor 00175 * @post new value of direction 00176 */ 00177 void mci_backward(void) 00178 { 00179 mci_direction = CCW; 00180 } 00181 00182 /* 00183 * @brief direction visualization 00184 * @pre initialization of motor 00185 * @post get direction value 00186 */ 00187 U8 mci_get_motor_direction(void) 00188 { 00189 return mci_direction; 00190 } 00191 00197 void mc_set_measured_period(U16 measured_period) 00198 { 00199 mci_measured_period = (mci_measured_period * 3 + measured_period * 4) / 4; 00200 } 00201 00208 U8 mci_get_measured_speed(void) 00209 { 00210 U16 measured_speed; 00211 00212 if (mci_measured_period == 0) {mci_measured_period += 1 ;} // warning DIV by 0 00213 measured_speed = (K_SPEED * 4)/ (mci_measured_period); 00214 if(measured_speed > 255) measured_speed = 255; // Variable saturation 00215 00216 return measured_speed; 00217 } 00218 00224 U16 mci_get_measured_current(void) 00225 { 00226 return mci_measured_current/64; 00227 } 00228 00234 void mci_store_measured_current(U16 current) 00235 { 00236 mci_measured_current = ((63*mci_measured_current)+(64*current))>>6; 00237 } 00238 00244 U8 mc_get_potentiometer_value(void) 00245 { 00246 return mc_potentiometer_value; 00247 } 00248 00254 void mc_set_potentiometer_value(U8 potentiometer) 00255 { 00256 mc_potentiometer_value = potentiometer; 00257 } 00258
1.5.7.1