The CMSIS DSP 5 library

Dependents:   Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more

Committer:
xorjoep
Date:
Thu Jun 21 11:56:27 2018 +0000
Revision:
3:4098b9d3d571
Parent:
1:24714b45cd1b
headers is a folder not a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xorjoep 1:24714b45cd1b 1 /* ----------------------------------------------------------------------
xorjoep 1:24714b45cd1b 2 * Project: CMSIS DSP Library
xorjoep 1:24714b45cd1b 3 * Title: arm_pid_init_q15.c
xorjoep 1:24714b45cd1b 4 * Description: Q15 PID Control initialization function
xorjoep 1:24714b45cd1b 5 *
xorjoep 1:24714b45cd1b 6 * $Date: 27. January 2017
xorjoep 1:24714b45cd1b 7 * $Revision: V.1.5.1
xorjoep 1:24714b45cd1b 8 *
xorjoep 1:24714b45cd1b 9 * Target Processor: Cortex-M cores
xorjoep 1:24714b45cd1b 10 * -------------------------------------------------------------------- */
xorjoep 1:24714b45cd1b 11 /*
xorjoep 1:24714b45cd1b 12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
xorjoep 1:24714b45cd1b 13 *
xorjoep 1:24714b45cd1b 14 * SPDX-License-Identifier: Apache-2.0
xorjoep 1:24714b45cd1b 15 *
xorjoep 1:24714b45cd1b 16 * Licensed under the Apache License, Version 2.0 (the License); you may
xorjoep 1:24714b45cd1b 17 * not use this file except in compliance with the License.
xorjoep 1:24714b45cd1b 18 * You may obtain a copy of the License at
xorjoep 1:24714b45cd1b 19 *
xorjoep 1:24714b45cd1b 20 * www.apache.org/licenses/LICENSE-2.0
xorjoep 1:24714b45cd1b 21 *
xorjoep 1:24714b45cd1b 22 * Unless required by applicable law or agreed to in writing, software
xorjoep 1:24714b45cd1b 23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
xorjoep 1:24714b45cd1b 24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
xorjoep 1:24714b45cd1b 25 * See the License for the specific language governing permissions and
xorjoep 1:24714b45cd1b 26 * limitations under the License.
xorjoep 1:24714b45cd1b 27 */
xorjoep 1:24714b45cd1b 28
xorjoep 1:24714b45cd1b 29 #include "arm_math.h"
xorjoep 1:24714b45cd1b 30
xorjoep 1:24714b45cd1b 31 /**
xorjoep 1:24714b45cd1b 32 * @addtogroup PID
xorjoep 1:24714b45cd1b 33 * @{
xorjoep 1:24714b45cd1b 34 */
xorjoep 1:24714b45cd1b 35
xorjoep 1:24714b45cd1b 36 /**
xorjoep 1:24714b45cd1b 37 * @details
xorjoep 1:24714b45cd1b 38 * @param[in,out] *S points to an instance of the Q15 PID structure.
xorjoep 1:24714b45cd1b 39 * @param[in] resetStateFlag flag to reset the state. 0 = no change in state 1 = reset the state.
xorjoep 1:24714b45cd1b 40 * @return none.
xorjoep 1:24714b45cd1b 41 * \par Description:
xorjoep 1:24714b45cd1b 42 * \par
xorjoep 1:24714b45cd1b 43 * The <code>resetStateFlag</code> specifies whether to set state to zero or not. \n
xorjoep 1:24714b45cd1b 44 * The function computes the structure fields: <code>A0</code>, <code>A1</code> <code>A2</code>
xorjoep 1:24714b45cd1b 45 * using the proportional gain( \c Kp), integral gain( \c Ki) and derivative gain( \c Kd)
xorjoep 1:24714b45cd1b 46 * also sets the state variables to all zeros.
xorjoep 1:24714b45cd1b 47 */
xorjoep 1:24714b45cd1b 48
xorjoep 1:24714b45cd1b 49 void arm_pid_init_q15(
xorjoep 1:24714b45cd1b 50 arm_pid_instance_q15 * S,
xorjoep 1:24714b45cd1b 51 int32_t resetStateFlag)
xorjoep 1:24714b45cd1b 52 {
xorjoep 1:24714b45cd1b 53
xorjoep 1:24714b45cd1b 54 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 55
xorjoep 1:24714b45cd1b 56 /* Run the below code for Cortex-M4 and Cortex-M3 */
xorjoep 1:24714b45cd1b 57
xorjoep 1:24714b45cd1b 58 /* Derived coefficient A0 */
xorjoep 1:24714b45cd1b 59 S->A0 = __QADD16(__QADD16(S->Kp, S->Ki), S->Kd);
xorjoep 1:24714b45cd1b 60
xorjoep 1:24714b45cd1b 61 /* Derived coefficients and pack into A1 */
xorjoep 1:24714b45cd1b 62
xorjoep 1:24714b45cd1b 63 #ifndef ARM_MATH_BIG_ENDIAN
xorjoep 1:24714b45cd1b 64
xorjoep 1:24714b45cd1b 65 S->A1 = __PKHBT(-__QADD16(__QADD16(S->Kd, S->Kd), S->Kp), S->Kd, 16);
xorjoep 1:24714b45cd1b 66
xorjoep 1:24714b45cd1b 67 #else
xorjoep 1:24714b45cd1b 68
xorjoep 1:24714b45cd1b 69 S->A1 = __PKHBT(S->Kd, -__QADD16(__QADD16(S->Kd, S->Kd), S->Kp), 16);
xorjoep 1:24714b45cd1b 70
xorjoep 1:24714b45cd1b 71 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
xorjoep 1:24714b45cd1b 72
xorjoep 1:24714b45cd1b 73 /* Check whether state needs reset or not */
xorjoep 1:24714b45cd1b 74 if (resetStateFlag)
xorjoep 1:24714b45cd1b 75 {
xorjoep 1:24714b45cd1b 76 /* Clear the state buffer. The size will be always 3 samples */
xorjoep 1:24714b45cd1b 77 memset(S->state, 0, 3U * sizeof(q15_t));
xorjoep 1:24714b45cd1b 78 }
xorjoep 1:24714b45cd1b 79
xorjoep 1:24714b45cd1b 80 #else
xorjoep 1:24714b45cd1b 81
xorjoep 1:24714b45cd1b 82 /* Run the below code for Cortex-M0 */
xorjoep 1:24714b45cd1b 83
xorjoep 1:24714b45cd1b 84 q31_t temp; /*to store the sum */
xorjoep 1:24714b45cd1b 85
xorjoep 1:24714b45cd1b 86 /* Derived coefficient A0 */
xorjoep 1:24714b45cd1b 87 temp = S->Kp + S->Ki + S->Kd;
xorjoep 1:24714b45cd1b 88 S->A0 = (q15_t) __SSAT(temp, 16);
xorjoep 1:24714b45cd1b 89
xorjoep 1:24714b45cd1b 90 /* Derived coefficients and pack into A1 */
xorjoep 1:24714b45cd1b 91 temp = -(S->Kd + S->Kd + S->Kp);
xorjoep 1:24714b45cd1b 92 S->A1 = (q15_t) __SSAT(temp, 16);
xorjoep 1:24714b45cd1b 93 S->A2 = S->Kd;
xorjoep 1:24714b45cd1b 94
xorjoep 1:24714b45cd1b 95
xorjoep 1:24714b45cd1b 96
xorjoep 1:24714b45cd1b 97 /* Check whether state needs reset or not */
xorjoep 1:24714b45cd1b 98 if (resetStateFlag)
xorjoep 1:24714b45cd1b 99 {
xorjoep 1:24714b45cd1b 100 /* Clear the state buffer. The size will be always 3 samples */
xorjoep 1:24714b45cd1b 101 memset(S->state, 0, 3U * sizeof(q15_t));
xorjoep 1:24714b45cd1b 102 }
xorjoep 1:24714b45cd1b 103
xorjoep 1:24714b45cd1b 104 #endif /* #if defined (ARM_MATH_DSP) */
xorjoep 1:24714b45cd1b 105
xorjoep 1:24714b45cd1b 106 }
xorjoep 1:24714b45cd1b 107
xorjoep 1:24714b45cd1b 108 /**
xorjoep 1:24714b45cd1b 109 * @} end of PID group
xorjoep 1:24714b45cd1b 110 */