/**********************************************************************
* *
* Software License Agreement *
* *
* The software supplied herewith by Microchip Technology *
* Incorporated (the "Company") for its dsPIC controller *
* is intended and supplied to you, the Company's customer, *
* for use solely and exclusively on Microchip dsPIC *
* products. The software is owned by the Company and/or its *
* supplier, and is protected under applicable copyright laws. All *
* rights are reserved. Any use in violation of the foregoing *
* restrictions may subject the user to criminal sanctions under *
* applicable laws, as well as to civil liability for the breach of *
* the terms and conditions of this license. *
* *
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO *
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, *
* BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE *
* COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, *
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. *
* *
**********************************************************************/
/**********************************************************************
* *
* Author: John Theys/Dave Ross *
* *
* Filename: ACIM.c *
* Date: 11/03/05 *
* File Version: 4.00 *
* *
* Tools used: MPLAB GL -> 6.30.0.0 *
* Compiler -> 1.22.3.0 *
* Assembler -> 1.22.3.0 *
* Linker -> 1.22.3.0 *
* *
* Linker File: p30f6010.gld *
* *
* *
***********************************************************************
*10/31/03 2.00 Released Motor runs fine, still some loose ends
*
*12/19/03 2.01 Cleaned up structure, created UserParms.h for all
* user defines.
*
*02/12/04 3.00 -Removed unnecessary files from project.
-Changed iRPM to int to correct floating point calc
* problems.
* -CalcVel() and velocity control loop only execute
* after number of loop periods specified by
* iIrpPerCalc.
* -Added iDispLoopCount variable to schedule
* execution of display and button routines
* -trig.s file changed to use program space for
* storage of sine data.
* -Added DiagnosticsOutput() function that uses
* output compare channels to output control variable
* information.
* -Added TORQUE_MODE definition to bypass velocity
* control loop.
* -Turned off SATDW bit in curmodel.s file. The
* automatic saturation feature prevents slip
* angle calculation from wrapping properly.
*
* 11/03/05 4.00 -Added support to software for MPLAB tuning
* interface. This includes the snapshot code.
* Also modified code so that user parameters
* only get initialized at user startup.
*
************************************************************************
* Code Description
*
* This file demonstrates Vector Control of a 3 phase ACIM using the
* dsPIC30F. SVM is used as the modulation strategy.
**********************************************************************
* Extention for sensorless control:
* Author: Dr.-Ing. Hafedh Sammoud APPCON Technologies SUARL
*
* Linker File: p30f6010A.gld
* 12 Feb. 2007 -FOC Control is extendet to work sensorless: Speed estimation
* module is calculated in "estim.c".
* - Switching between sensored control and sensorless control
* by toggeling S7
* - Start up strategie and low speed limitation when sensorless
* control is selected
* - Offset compensation is disactivated for SL-Control
* - Display indicates wich mode is selected */
/************** GLOBAL DEFINITIONS ***********/
#define INITIALIZE
#include "Motor.h"
#include "Parms.h"
#include "Encoder.h"
#include "SVGen.h"
#include "ReadADC.h"
#include "MeasCurr.h"
#include "CurModel.h"
#include "FdWeak.h"
#include "Control.h"
#include "Controldef.h"
#include "PI.h"
#include "Park.h"
#include "ParkDef.h"
#include "OpenLoop.h"
#include "LCD.h"
#include "bin2dec.h"
#include "UserParms.h"
#include "Estim.h"
/*****************Config bit settings****************/
_FOSC(CSW_FSCM_OFF & XT_PLL16);
//_FOSC(CSW_FSCM_OFF & XT_PLL8);
//_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_ON & BORV_20 & PWRT_64 & MCLR_EN);
/************** END OF GLOBAL DEFINITIONS ***********/
//#define TORQUE_MODE
//#define DIAGNOSTICS
//#define SNAPSHOT
#ifdef SNAPSHOT
#define SNAPSIZE 900
#define SNAP_DELAY SNAPSIZE/4
#define SNAP1 ParkParm.qIq
#define SNAP2 ParkParm.qId
#define SNAP3 EncoderParm.qVelMech
int SnapBuf1[SNAPSIZE];
int SnapBuf2[SNAPSIZE];
int SnapBuf3[SNAPSIZE];
int SnapCount;
#endif
unsigned short uWork;
short iCntsPerRev;
short iDeltaPos;
union {
struct
{
unsigned DoLoop :1;
unsigned BeginStop :1;
unsigned DoSnap :1;
unsigned SnapDone :1;
unsigned OpenLoop :1;
unsigned RunMotor :1;
unsigned RunningUpload :1;
unsigned Btn1Pressed :1;
unsigned Btn2Pressed :1;
unsigned Btn3Pressed :1;
unsigned Btn4Pressed :1;
unsigned UploadToMonitor:1;
unsigned ChangeMode :1;
unsigned ChangeSpeed :1;
unsigned Sensorless :1;
unsigned :1;
}bit;
WORD Word;
} uGF; // general flags
union {
long Long;
WORD Word[2];
short Short[2];
BYTE Byte[4];
} DataUnion;
tPIParm PIParmQ;
tPIParm PIParmQref;
tPIParm PIParmD;
tReadADCParm ReadADCParm;
int iRPM,iRPM_old;
WORD iMaxLoopCnt;
WORD iLoopCnt;
WORD iDispLoopCnt;
extern tEstimParm EstimParm;
extern tStartupParm StartupParm;
/******************************************************/
void __attribute__((__interrupt__)) _ADCInterrupt(void);
void SetupBoard( void );
bool SetupPeripherals(void);
void InitUserParms(void);
void DoControl( void );
void Dis_RPM( BYTE bChrPosC, BYTE bChrPosR );
void DiagnosticsOutput(void);
/************* START OF MAIN FUNCTION ***************/
int main ( void )
{
// InitEstimParmLeeson();
InitEstimParm0();
SetupPorts();
InitLCD();
InitUserParms();
w