
unit motor
{
	public const uint16 ID = 0;
	
	public link unit STEP {}
	public link unit DIR {}
	public link unit ENABLE {}
	
	//[i] var_uint8 OS_time;
	uint8 OS_time;

	//[i] linkconst_int32_200_步/每秒 speed;
	public const int32 speed = 0;
	int16 myspeed;
	int16 realspeed;
	
	int16 offset;
	
	//方向  0:停止  1:正转  2:反转
	uint8 tDIR;
	
	int32 time;
	
	int32 addtime;
	
	int32 dline;
	
	bool accel;
	bool runforever;
	
	//----------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		STEP.D0_DIR = 1;
		STEP.D0_OUT = 0;
		DIR.D0_DIR = 1;
		DIR.D0_OUT = 0;
		ENABLE.D0_DIR = 1;
		ENABLE.D0_OUT = 0;
		
		OS_time = 1;//(uint8)(uint16)(uint)Time;
		
		tDIR = 0;
		offset = 0;
		
		addtime = 0;
		time = 0;
		
		set_speed( speed );
		
		accel = true;
	}
	//----------------------------------------------------
	//[i] function_void OS_run;
	
	#include "$run$.txt"
	
	//----------------------------------------------------
	//[i] function_void set_speed int32;
	public void set_speed( int32 s )
	{
		#asm "cli"
		myspeed = (int16)s;
		realspeed = myspeed;
		dline = s * s / 2000;
		#asm "sei"
	}
	
	//----------------------------------------------------
	//[i] function_void run_right;
	public void run_right()
	{
		#asm "cli"
		tDIR = 1;
		time = 0;
		DIR.D0_OUT = 0;
		addtime = 0;
		realspeed = 0;
		runforever = true;
		#asm "sei"
	}
	//[i] function_void run_left;
	public void run_left()
	{
		#asm "cli"
		tDIR = 2;
		time = 0;
		DIR.D0_OUT = 1;
		addtime = 0;
		realspeed = 0;
		runforever = true;
		#asm "sei"
	}
	//[i] function_void run_right_t int32;
	public void run_right_t( int32 t )
	{
		#asm "cli"
		time = t;
		tDIR = 1;
		DIR.D0_OUT = 0;
		addtime = 0;
		realspeed = 0;
		runforever = false;
		#asm "sei"
	}
	//[i] function_void run_left_t int32;
	public void run_left_t( int32 t )
	{
		#asm "cli"
		time = t;
		tDIR = 2;
		DIR.D0_OUT = 1;
		addtime = 0;
		realspeed = 0;
		runforever = false;
		#asm "sei"
	}
	//[i] function_void stop;
	public void stop()
	{
		#asm "cli"
		if( !accel ) {
			tDIR = 0;
		}
		else {
			time = dline - 1;
			runforever = false;
		}
		#asm "sei"
	}
	//[i] function_void short_stop;
	public void short_stop()
	{
		#asm "cli"
		tDIR = 0;
		#asm "sei"
	}
	
	//[i] function_bool Running;
	public bool Running()
	{
		return tDIR != 0;
	}
	//[i] function_bool Stoped;
	public bool Stoped()
	{
		return tDIR == 0;
	}
	
	//=================================================================================
	//[i] function_void power_on;
	public void power_on()
	{
		ENABLE.D0_OUT = 0;
	}
	//[i] function_void power_off;
	public void power_off()
	{
		ENABLE.D0_OUT = 1;
	}
	
	//[i] function_void accel_on;
	public void accel_on()
	{
		accel = true;
	}
	//[i] function_void accel_off;
	public void accel_off()
	{
		accel = false;
	}
}







