Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 6 → Rev 7

/8SMC1-USBhF/usmctest/libusmc.h
0,0 → 1,161
#ifndef _USMCDLL_H
#define _USMCDLL_H
 
 
#include <stdlib.h>
 
 
#define USMC_ERROR_REF_DEV_DISCONNECTED 0x14141413
#define USMC_ERROR_NOT_INITIALIZED 0x14141414
#define USMC_ERROR_NO_DEVICES_CONNECTED 0x14141415
#define USMC_SUCCESS 0
#define TRUE 1
#define FALSE 0
 
 
typedef unsigned char BYTE;
typedef unsigned int DWORD;
typedef unsigned short WORD;
typedef unsigned int BOOL;
typedef void * PVOID;
 
 
typedef struct _USMC_Devices
{
DWORD NOD; // Number of devices connected to computer.
char ** Serial; // Array of pointers to 16√byte ASCII strings of length √ NOD.
char ** Version; // Array of pointers to 4√byte ASCII strings of length √ NOD.
} USMC_Devices, * PUSMC_Devices, * LPUSMC_Devices;
 
 
typedef struct _USMC_Parameters
{
float AccelT; // Acceleration time (in ms).
float DecelT; // Deceleration time (in ms).
float PTimeout; // Time (in ms) after which current will be reduced to 60% of normal.
float BTimeout1; // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO1P field in this structure.
float BTimeout2; // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO2P field in this structure.
float BTimeout3; // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO3P field in this structure.
float BTimeout4; // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO4P field in this structure.
float BTimeoutR; // Time (in ms) after which reset command will be performed.
float BTimeoutD; // This field is reserved for future use.
float MinP; // Speed (steps/sec) while performing reset operation.
float BTO1P; // Speed (steps/sec) after BTIMEOUT1 time has passed.
float BTO2P; // Speed (steps/sec) after BTIMEOUT2 time has passed.
float BTO3P; // Speed (steps/sec) after BTIMEOUT3 time has passed.
float BTO4P; // Speed (steps/sec) after BTIMEOUT4 time has passed.
WORD MaxLoft; // Value in full steps that will be used performing backlash operation.
DWORD StartPos; // Current Position saved to FLASH.
WORD RTDelta; // Revolution distance √ number of full steps per one full revolution.
WORD RTMinError; // Number of full steps missed to raise the error flag.
float MaxTemp; // Maximum allowed temperature (centigrade degrees).
BYTE SynOUTP; // Duration of the output synchronization pulse.
float LoftPeriod; // Speed (steps/sec) of the last phase of the backlash operation.
float EncMult; // Encoder step multiplier. Should be <Encoder Steps per Evolution> / <SM Steps per Evolution> and should be integer multiplied by 0.25.
} USMC_Parameters, * PUSMC_Parameters, * LPUSMC_Parameters;
 
 
typedef struct _USMC_StartParameters
{
BYTE SDivisor; // Step is divided by this factor (1,2,4,8).
BOOL DefDir; // Direction for backlash operation (relative).
BOOL LoftEn; // Enable automatic backlash operation (works if slow start/stop mode is off).
BOOL SlStart; // If TRUE slow start/stop mode enabled.
BOOL WSyncIN; // If TRUE controller will wait for input synchronization signal to start.
BOOL SyncOUTR; // If TRUE output synchronization counter will be reset.
BOOL ForceLoft; // If TRUE and destination position is equal to the current position backlash operation will be performed.
} USMC_StartParameters, * PUSMC_StartParameters, * LPUSMC_StartParameters;
 
 
typedef struct _USMC_Mode
{ // Masked Saved To Flash Description:
BOOL PMode; // √ YES Turn off buttons (TRUE - buttons disabled).
BOOL PReg; // √ YES Current reduction regime (TRUE - regime is on).
BOOL ResetD; // √ YES Turn power off and make a whole step (TRUE - apply).
BOOL EMReset; // √ √ Quick power off.
BOOL Tr1T; // √ YES Trailer 1 TRUE state (TRUE : +3/+5б; FALSE : 0б).
BOOL Tr2T; // √ YES Trailer 2 TRUE state (TRUE : +3/+5б; FALSE : 0б).
BOOL RotTrT; // √ YES Rotary Transducer TRUE state (TRUE : +3/+5б; FALSE : 0б).
BOOL TrSwap; // √ YES If TRUE, Trailers are treated to be swapped.
BOOL Tr1En; // √ YES If TRUE Trailer 1 Operation Enabled.
BOOL Tr2En; // √ YES If TRUE Trailer 2 Operation Enabled.
BOOL RotTeEn; // √ YES If TRUE Rotary Transducer Operation Enabled.
BOOL RotTrOp; // √ YES Rotary Transducer Operation Select (stop on error if TRUE).
BOOL Butt1T; // √ YES Button 1 TRUE state (TRUE : +3/+5б; FALSE : 0б).
BOOL Butt2T; // √ YES Button 2 TRUE state (TRUE : +3/+5б; FALSE : 0б).
BOOL ResetRT; // YES √ Reset Rotary Transducer Check Positions (need one full revolution before it can detect error).
BOOL SyncOUTEn; // √ YES If TRUE output synchronization enabled.
BOOL SyncOUTR; // YES √ If TRUE output synchronization counter will be reset.
BOOL SyncINOp; // √ YES Synchronization input mode:
// TRUE - Step motor will move one time to the DestPos
// FALSE - Step motor will move multiple times by DestPos microsteps as distance.
DWORD SyncCount; // √ YES Number of steps after which synchronization output signal occurs.
BOOL SyncInvert; // √ YES Set this bit to TRUE to invert output synchronization polarity.
BOOL EncoderEn; // √ YES Enable Encoder on pins {SYNCIN,ROTTR} - disables Synchronization input and Rotary Transducer.
BOOL EncoderInv; // √ YES Invert Encoder Counter Direction.
BOOL ResBEnc; // YES √ Reset <EncoderPos> and <ECurPos> to 0.
BOOL ResEnc; // YES √ Reset <ECurPos> to <EncoderPos>.
} USMC_Mode, * PUSMC_Mode, * LPUSMC_Mode;
 
 
typedef struct _USMC_State
{
int CurPos; // Current position (in 1/8 steps).
float Temp; // Current temperature of the power driver.
BYTE SDivisor; // Step is divided by this factor.
BOOL Loft; // Indicates backlash status.
BOOL FullPower; // Full power if TRUE.
BOOL CW_CCW; // Current direction of rotation (relatively to some direction √ dependent on step motor circuits connection and on its construction).
BOOL Power; // If TRUE then Step Motor power is ON.
BOOL FullSpeed; // If TRUE then full speed. Valid in "Slow Start" mode only.
BOOL AReset; // TRUE After Device reset, FALSE after "Set Position".
BOOL RUN; // TRUE if step motor is rotating.
BOOL SyncIN; // Logical state directly from input synchronization PIN (pulses treated as positive).
BOOL SyncOUT; // Logical state directly from output synchronization PIN (pulses are positive).
BOOL RotTr; // Indicates current rotary transducer logical press state.
BOOL RotTrErr; // Indicates rotary transducer error flag (reset by USMC_SetMode function with ResetRT bit √ TRUE).
BOOL EmReset; // Indicates state of emergency disable button (TRUE √ Step motor power off).
BOOL Trailer1; // Indicates trailer 1 logical press state.
BOOL Trailer2; // Indicates trailer 2 logical press state.
float Voltage; // Input power source voltage (6-39V) -= 24 version 0nly=-.
} USMC_State, * PUSMC_State, * LPUSMC_State;
 
 
typedef struct _USMC_EncoderState
{
int EncoderPos; // Current position measured by encoder.
int ECurPos; // Current position (in Encoder Steps) - Synchronized with request call.
} USMC_EncoderState, * PUSMC_EncoderState, * LPUSMC_EncoderState;
 
 
typedef USMC_Devices const * PCUSMC_Devices;
typedef USMC_Devices const * LPCUSMC_Devices;
typedef USMC_Parameters const * PCUSMC_Parameters;
typedef USMC_Parameters const * LPCUSMC_Parameters;
typedef USMC_StartParameters const * PCUSMC_StartParameters;
typedef USMC_StartParameters const * LPCUSMC_StartParameters;
typedef USMC_Mode const * PCUSMC_Mode;
typedef USMC_Mode const * LPCUSMC_Mode;
typedef USMC_State const * PCUSMC_State;
typedef USMC_State const * LPCUSMC_State;
typedef USMC_EncoderState const * PCUSMC_EncoderState;
typedef USMC_EncoderState const * LPCUSMC_EncoderState;
 
 
DWORD USMC_Init ( USMC_Devices * Str );
DWORD USMC_GetState ( DWORD Device, USMC_State * Str );
DWORD USMC_SaveParametersToFlash ( DWORD Device );
DWORD USMC_GetMode ( DWORD Device, USMC_Mode * Str );
DWORD USMC_SetMode ( DWORD Device, USMC_Mode * Str );
DWORD USMC_GetParameters ( DWORD Device, USMC_Parameters * Str );
DWORD USMC_SetParameters ( DWORD Device, USMC_Parameters * Str );
DWORD USMC_GetStartParameters ( DWORD Device, USMC_StartParameters * Str );
DWORD USMC_Start ( DWORD Device, int DestPos,
float * Speed,
USMC_StartParameters * Str );
DWORD USMC_Stop ( DWORD Device );
DWORD USMC_SetCurrentPosition ( DWORD Device, int Position );
DWORD USMC_GetEncoderState ( DWORD Device, USMC_EncoderState * Str );
void USMC_GetLastErr ( char * str, ssize_t len );
DWORD USMC_Close ( void );
#endif // _USMCDLL_H
/8SMC1-USBhF/usmctest/usmctest.c
0,0 → 1,658
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "libusmc.h"
 
 
#define MENU_EXIT 0
#define MENU_ERROR 1
#define MENU_CONTINUE 2
 
USMC_Devices devices;
DWORD cur_dev;
 
char str [105] = { 100 };
 
// Function that prints information about connected devices to console
void print_devices ( USMC_Devices devices )
{
DWORD i;
 
for ( i = 0 ; i < devices.NOD ; i++ )
printf ( "Device# %d,\tSerial Number - %.16s,\tVersion - %.4s\n", i + 1, devices.Serial [i], devices.Version [i] );
}
 
// Function that prints information about device state to console
void print_state ( USMC_State state )
{
printf ( "The state is:\n" );
printf ( "- Current Position in microsteps - %d\n", state.CurPos );
printf ( "- Temperature - %.2f\xf8\x43\n", state.Temp );
printf ( "- Step Divisor - %d\n", state.SDivisor );
printf ( "- Loft state - %s\n", state.Loft?"Indefinite" : "Fixed" );
printf ( "- Power - %s\n", state.Power ? ( state.FullPower ? "Full" : "Half" ) : "Off" );
 
if ( state.RUN )
printf ( "- Step Motor is Running in %s Direction %s\n",
state.CW_CCW ? "CCW" : "CW", ( ( state.SDivisor == 1 ) && state.FullSpeed ) ? "at Full speed" : "" );
else
printf ( "- Step Motor is Not Running\n" );
 
printf ( "- Device %s\n", state.AReset ? "is After Reset" : "Position Already Set" );
printf ( "- Input Synchronization Logical Pin state - %s\n", state.SyncIN ? "TRUE" : "FALSE" );
printf ( "- Output Synchronization Logical Pin state - %s\n", state.SyncOUT ? "TRUE" : "FALSE" );
printf ( "- Rotary Transducer Logical Pin state - %s\n", state.RotTr ? "TRUE" : "FALSE" );
printf ( "- Rotary Transducer Error Flag - %s\n", state.RotTrErr ? "Error" : "Clear" );
printf ( "- Emergency Disable Button - %s\n", state.EmReset ? "Pushed" : "Unpushed" );
printf ( "- Trailer 1 Press state - %s\n", state.Trailer1 ? "Pushed" : "Unpushed" );
printf ( "- Trailer 2 Press state - %s\n", state.Trailer2 ? "Pushed" : "Unpushed" );
 
if ( state.Voltage == 0.0f )
printf ( "- Input Voltage - Low\n", state.Voltage );
else
printf ( "- Input Voltage - %.1fV\n", state.Voltage );
}
 
// Function that scans start parameters
void scan_start_params ( int * dest_pos, float * speed, USMC_StartParameters * sp )
{
// Defaults
*speed = 2000.0f;
*dest_pos = 0;
sp -> SDivisor = 8;
 
printf ( "Destination position:" );
scanf ( "%d", dest_pos );
printf ( "\nSpeed (in tacts):" );
scanf ( "%f", speed );
printf ( "\nSteps Divisor:" );
scanf ( "%d", &( sp -> SDivisor ) );
getchar ();
}
 
// Function that prints information about device start parameters to console
void print_start_params ( int dest_pos, float speed, const USMC_StartParameters sp )
{
printf ( "Destination position - %d\n", dest_pos );
printf ( "speed - %.2ftacts/s\n", speed );
printf ( "Steps Divisor - %d\n", sp.SDivisor );
 
if ( sp.SDivisor == 1 )
printf ( "Slow start/stop mode - %s\n", sp.SlStart ? "Enabled" : "Disabled" );
else if ( sp.LoftEn ) {
printf ( "Automatic backlash operation - Enabled\n" );
printf ( "Automatic backlash operation direction - %s\n", sp.DefDir ? "CCW" : "CW" );
printf ( "Force automatic backlash operation - %s\n", sp.ForceLoft ? "TRUE" : "FALSE" );
} else {
printf ( "Automatic backlash operation - Disabled\n" );
}
if ( sp.WSyncIN )
printf ( "Controller will wait for input synchronization signal to start\n" );
else
printf ( "Input synchronization signal ignored \n" );
 
printf ( "Output synchronization counter will %sbe reset\n", sp.SyncOUTR ? "" : "not " );
}
 
// Function that prints information about device parameters to console
void print_params ( USMC_Parameters params )
{
printf ( "The parameters are:\n" );
printf ( "Full acceleration time - %.0f ms\n", ( double ) params.AccelT );
printf ( "Full deceleration time - %.0f ms\n", ( double ) params.DecelT );
printf ( "Power reduction timeout - %.0f ms\n", ( double ) params.PTimeout );
printf ( "Button speedup timeout 1 - %.0f ms\n", ( double ) params.BTimeout1 );
printf ( "Button speed after timeout 1 - %.2f steps/s\n", ( double ) params.BTO1P );
printf ( "Button speedup timeout 2 - %.0f ms\n", ( double ) params.BTimeout2 );
printf ( "Button speed after timeout 2 - %.2f steps/s\n", ( double ) params.BTO2P );
printf ( "Button speedup timeout 3 - %.0f ms\n", ( double ) params.BTimeout3 );
printf ( "Button speed after timeout 3 - %.2f steps/s\n", ( double ) params.BTO3P );
printf ( "Button speedup timeout 4 - %.0f ms\n", ( double ) params.BTimeout4 );
printf ( "Button speed after timeout 4 - %.2f steps/s\n", ( double ) params.BTO4P );
printf ( "Button reset timeout - %.0f ms\n", (double) params.BTimeoutR );
printf ( "Button reset operation speed - %.2f steps/s\n", ( double ) params.MinP );
printf ( "Backlash operation distance - %d steps\n", ( int ) params.MaxLoft );
printf ( "Revolution distance - %d steps\n", ( int ) params.RTDelta );
printf ( "Minimal revolution distance error - %d steps\n", ( int ) params.RTMinError );
printf ( "Power off temperature - %.2f\xf8\x43\n", ( double ) params.MaxTemp );
printf ( "Duration of the output synchronization pulse - " );
 
if ( params.SynOUTP == 0 )
printf ( "minimal\n");
else
printf ( "%.1f * [Tact Period]\n", params.SynOUTP - 0.5 );
 
printf ( "speed of the last phase of the backlash operation - " );
 
if ( params.LoftPeriod == 0.0f )
printf ( "normal\n" );
else
printf ( "%.2f steps/s\n", ( double ) params.LoftPeriod );
 
printf ( "<Angular Encoder Step> Equals <Angular Step Motor Step>/<%.2f>\n", params.EncMult );
}
 
// Function that prints information about device "mode" parameters to console
void print_mode ( USMC_Mode mode )
{
printf ( "mode parameters:\n" );
printf ( "Buttons - ");
 
if ( mode.PMode )
printf ( "Disabled\n" );
else {
printf ( "Enabled\nButton 1 TRUE state - %s\n", mode.Butt1T ? "+3/+5 V" : "0 V(GND)" );
printf ( "Button 2 TRUE state - %s\n", mode.Butt2T ? "+3/+5 V" : "0 V(GND)" );
}
 
printf ( "Current reduction regime - %s\n", mode.PReg ? "Used" : "Not Used" );
 
if ( mode.ResetD )
printf ( "Power - %s\n", mode.EMReset ? "Emerjency Off" : "Off" );
else
printf ( "Power - On\n" );
 
if ( mode.Tr1En || mode.Tr2En )
printf ( "Trailers are - %s\n", mode.TrSwap ? "Swapped" : "Direct" );
 
printf ( "Trailer 1 - ");
 
if ( mode.Tr1En )
printf ( "Enabled\nTrailer 1 TRUE state - %s\n", mode.Tr1T ? "+3/+5 V" : "0 V(GND)" );
else
printf ( "Disabled\n" );
 
printf ( "Trailer 2 - " );
 
if ( mode.Tr2En )
printf ( "Enabled\nTrailer 2 TRUE state - %s\n", mode.Tr2T ? "+3/+5 V" : "0 V(GND)" );
else
printf ( "Disabled\n" );
 
if ( mode.EncoderEn ) {
printf ( "Encoder - Enabled\n" );
printf ( "Encoder Position Counter is %s\n", mode.EncoderInv ? "Inverted" : "Direct" );
printf ( "Rotary Transducer and Input Syncronisation are\n"
" Disabled Because of Encoder\n" );
} else {
printf ( "Encoder - Disabled\n" );
printf ( "Rotary Transducer - " );
 
if ( mode.RotTeEn ) {
printf ( "Enabled\nRotary Transducer TRUE state - %s\n", mode.RotTrT ? "+3/+5 V" : "0 V(GND)" );
printf ( "Rotary Transducer Operation - %s\n", mode.RotTrOp ? "Stop on error" : "Check and ignore error" );
printf ( "Reset Rotary Transducer Check Positions - %s\n", mode.ResetRT ? "Initiated" : "No, why?" );
} else {
printf ( "Disabled\n" );
}
 
printf ( "Synchronization input mode:\n" );
 
if ( mode.SyncINOp )
printf ( "Step motor will move one time to the destination position\n" );
else
printf ( "Step motor will move multiple times by [destination position]\n" );
}
 
printf ( "Output Syncronization - " );
 
if ( mode.SyncOUTEn ) {
printf ( "Enabled\nReset Output Synchronization Counter - %s\n", mode.SyncOUTR ? "Initiated" : "No, why?" );
printf ( "Number of steps after which synchronization output sygnal occures - %u\n", mode.SyncCount );
} else {
printf ( "Disabled\n" );
}
 
printf ( "Synchronization Output is " );
 
if ( mode.SyncInvert )
printf ( "INVERTED\n" );
else
printf ( "NORMAL\n" );
}
 
void print_enc_state ( USMC_EncoderState enc_state, USMC_Parameters up )
{
printf ( "The encoder state is:\n" );
printf ( "- Current Position in microsteps - %.2f\n", enc_state.ECurPos/up.EncMult );
printf ( "- Encoder Position in microsteps - %.2f\n\n", enc_state.EncoderPos/up.EncMult );
printf ( "- Current Position in \"Half of Encoder Step\"s - %d\n", enc_state.ECurPos );
printf ( "- Encoder Position in \"Half of Encoder Step\"s - %d\n", enc_state.EncoderPos );
}
 
// Function that prints last error information
void print_error ( void )
{
char er [256];
 
USMC_GetLastErr ( er, 255 );
er [255] = '\0';
printf ( "\n%s", er );
}
 
 
USMC_State state;
USMC_StartParameters start_params;
USMC_Parameters params;
USMC_Mode mode;
USMC_EncoderState enc_state;
 
// Reference to Functions For Every Command (defined at the end of the file)
BOOL f1_get_dev_state ( void );
BOOL f2_start ( void );
BOOL f3_stop ( void );
BOOL f4_set_params ( void );
BOOL f5_set_mode ( void );
BOOL f6_set_cur_pos ( void );
BOOL f7_turn_off_and_save_pos ( void );
BOOL fs7_revert_sp_to_0 ( void );
BOOL f8_get_enc_state ( void );
BOOL fp_change_power ( void );
 
BOOL smpower = FALSE;
 
int menu ( void )
{
BOOL err = FALSE;
char menu_sel;
 
printf ( "menu :\n"
"1 - Get Device state\n"
"2 - START\n"
"3 - STOP\n"
"4 - Set Parameters (can be changed only in c++ code)\n"
"5 - Set mode (can be changed only in c++ code)\n"
"6 - Set Current Position (can be changed only in c++ code)\n"
"7 - Turn Off and Save Current Position to Flash\n"
"SHIFT + 7 - Revert Start Position to 0\n"
"8 - Get Encoder state\n"
"\n"
"p - Turn %s Power"
"\n"
"9 - Select other device\n"
"0 - Exit program\n"
"Choose:", smpower ? "Off" : "On" );
 
menu_sel = getchar ();
 
switch ( menu_sel )
{
case '1':
err = f1_get_dev_state ();
break;
case '2':
err = f2_start ();
break;
case '3':
err = f3_stop ();
break;
case '4':
err = f4_set_params ();
break;
case '5':
err = f5_set_mode ();
break;
case '6':
err = f6_set_cur_pos ();
break;
case '7':
err = f7_turn_off_and_save_pos ();
break;
case '&': // 'SHIFT 7' on my keyboard
err = fs7_revert_sp_to_0 ();
break;
case '8':
err = f8_get_enc_state ();
break;
case 'p':
case 'P':
err = fp_change_power ();
break;
 
case '9':
return MENU_ERROR;
case '0':
printf ( "\n" );
return MENU_EXIT;
default:
system ( "clear" );
break;
}
 
if ( err ) {
print_error ();
printf ( "\nPerforming Refressh..." );
getchar ();
 
if ( USMC_Init ( &devices ) ) {
print_error ();
return MENU_EXIT;
}
 
return MENU_ERROR;
}
 
return MENU_CONTINUE;
}
 
int select_menu ( void )
{
int ret;
do {
system ( "clear" );
cur_dev = -1;
print_devices ( devices );
printf ( "\n\t0\tExit\n" );
for ( ret=0;ret<devices.NOD;ret++) printf ( "\t%d\tDevice # %d\n",ret+1,ret+1 );
printf ( "\tx\tReInitialize\n" );
printf ( "\txx\tClose Driver Window and Exit Program\n" );
 
printf ( "Select:" );
scanf ( "%s", str );
getchar ();
 
if ( strcmp ( str, "x" ) == 0 )
{
//// Perform "Refresh" of Driver
if ( USMC_Init ( &devices ) )
{
print_error ();
return MENU_EXIT;
}
 
continue;
} else if ( strcmp ( str, "xx" ) == 0 ) {
//// Close the MicroSMC.exe process
if ( USMC_Close () )
{
print_error ();
return MENU_EXIT;
}
 
return MENU_EXIT;
}
 
sscanf ( str, "%u", &cur_dev );
 
if ( cur_dev == 0 )
return MENU_EXIT;
} while ( cur_dev > devices.NOD );
 
system ( "clear" );
 
cur_dev--; // Decrement cur_dev by 1 ( we have zero-based device list,
// but 1-based is user-friendly )
 
do {
ret = menu ();
} while ( ret == MENU_CONTINUE );
 
return ret;
}
 
int exit_program ( void )
{
printf ( "\nPress any key to exit" );
getchar ();
 
return MENU_EXIT;
}
 
int main ( int argc, const char ** argv )
{
int res;
 
// change terminal mode to non-buffering
system ( "stty -icanon min 1" );
setbuf ( stdin, NULL );
system ( "clear" );
 
if ( USMC_Init ( &devices ) )
{
print_error ();
 
return exit_program ();
}
 
while ( select_menu () );
 
res = exit_program ();
//restore terminal mode
system ( "stty -icanon" );
 
return res;
}
 
 
 
BOOL f1_get_dev_state ( void )
{
if ( USMC_GetState ( cur_dev, &state ) )
return TRUE;
 
system ( "clear" );
print_state ( state );
printf ( "\nPress any key to continue" );
getchar ();
system ( "clear" );
 
return FALSE;
}
 
BOOL f2_start ( void )
{
float speed = 2000.0f;
int dest_pos = 0;
 
if ( USMC_GetStartParameters ( cur_dev, &start_params ) )
return TRUE;
 
system ( "clear" );
scan_start_params ( &dest_pos, &speed, &start_params );
start_params.SlStart = TRUE;
 
if ( USMC_Start ( cur_dev, dest_pos, &speed, &start_params ) )
return TRUE;
system ( "clear" );
print_start_params ( dest_pos, speed, start_params );
printf ( "\n" );
return FALSE;
}
 
BOOL f3_stop ( void )
{
if ( USMC_Stop ( cur_dev ) )
return TRUE;
system ( "clear" );
printf ( "Motor stopped\n\n" );
 
return FALSE;
}
 
BOOL f4_set_params ( void )
{
if ( USMC_GetParameters ( cur_dev, &params ) )
return TRUE;
 
// Set anything you want here
params.MaxTemp = 70.0f;
params.AccelT = 200.0f;
params.DecelT = 200.0f;
params.BTimeout1 = 5000.0f;
params.BTimeout2 = 5000.0f;
params.BTimeout3 = 5000.0f;
params.BTimeout4 = 5000.0f;
params.BTO1P = 625.0f;
params.BTO2P = 16.0f;
params.BTO3P = 625.0f;
params.BTO4P = 16.0f;//500.0f;
params.MinP = 500.0f;
params.BTimeoutR = 500.0f;
params.LoftPeriod = 500.0f;
params.RTDelta = 200;
params.RTMinError = 15;
params.EncMult = 2.5f;
params.MaxLoft = 32;
params.PTimeout = 100.0f;
params.SynOUTP = 1;
//
 
if ( USMC_SetParameters ( cur_dev, &params ) )
return TRUE;
 
if ( USMC_SaveParametersToFlash ( cur_dev ) )
return TRUE;
 
system ( "clear" );
print_params ( params );
printf ( "\nThese Parameters are Saved to Flash" );
printf ( "\nPress any key to continue" );
getchar ();
system ( "clear" );
return FALSE;
}
 
BOOL f5_set_mode ( void )
{
if ( USMC_GetMode ( cur_dev, &mode ) )
return TRUE;
 
// Set anything you want here
mode.SyncInvert = !mode.SyncInvert;
mode.EncoderEn = TRUE;
mode.RotTrOp = FALSE;
mode.ResetRT = TRUE;
mode.ResBEnc = TRUE;
// Set anything you want here
 
if ( USMC_SetMode ( cur_dev, &mode ) )
return TRUE;
 
system ( "clear" );
print_mode ( mode );
printf ( "\nPress any key to continue" );
getchar ();
system ( "clear" );
return FALSE;
}
 
BOOL f6_set_cur_pos ( void )
{
int newCurPos = 1000;
 
if ( USMC_SetCurrentPosition ( cur_dev, newCurPos ) )
return TRUE;
system ( "clear" );
printf ( "\nSetCurrentPosition executed with argument %d", newCurPos );
printf ( "\nPress any key to continue" );
getchar ();
system ( "clear" );
return FALSE;
}
 
BOOL f7_turn_off_and_save_pos ( void )
{
// Initialize structures (in case this function runs first)
if ( USMC_GetParameters ( cur_dev, &params ) )
return TRUE;
if ( USMC_GetMode ( cur_dev, &mode ) )
return TRUE;
 
// Go to Full Step (automaticaly), then Turn Off
mode.ResetD = TRUE;
 
if ( USMC_SetMode ( cur_dev, &mode ) )
return TRUE;
 
// Wait until Previous Comand is Done
do {
usleep ( 50 );
 
if ( USMC_GetState ( cur_dev, &state ) )
return TRUE;
} while ( state.Power == TRUE );
 
// Remember CurPos in Parameters Only While state.Power - FALSE
params.StartPos = state.CurPos;
 
if ( USMC_SetParameters ( cur_dev, &params ) )
return TRUE;
 
// Then of Course You Need to SaveToFlash
if ( USMC_SaveParametersToFlash ( cur_dev ) )
return TRUE;
 
// Now You Can Unplug ME
system ( "clear" );
printf ( "\nThe Position \"%d\" is Saved to Flash\n", params.StartPos );
printf ( "\nWhen Controller is Powered Up Next Time\n" );
printf ( "\nIt Will Start From This Position\n" );
printf ( "\nPress any key to exit" );
getchar ();
system ( "clear" );
return FALSE;
}
 
BOOL fs7_revert_sp_to_0 ( void )
{
// Initialize structures (in case this function runs first)
if ( USMC_GetParameters ( cur_dev, &params ) )
return TRUE;
 
params.StartPos = 0;
 
if ( USMC_SetParameters ( cur_dev, &params ) )
return TRUE;
 
// Then of Course You Need to SaveToFlash
if ( USMC_SaveParametersToFlash ( cur_dev ) )
return TRUE;
system ( "clear" );
printf ( "\nStart Position is Reset to 0\n" );
printf ( "\nPress any key to continue" );
getchar ();
 
return FALSE;
}
 
BOOL f8_get_enc_state ( void )
{
if ( USMC_GetEncoderState ( cur_dev, &enc_state ) )
return TRUE;
if ( USMC_GetParameters ( cur_dev, &params ) )
return TRUE;
system ( "clear" );
print_enc_state ( enc_state, params );
printf ( "\nPress any key to continue" );
getchar ();
system ( "clear" );
 
return FALSE;
}
 
BOOL fp_change_power ( void )
{
if ( USMC_GetMode ( cur_dev, &mode ) )
return TRUE;
 
smpower = !smpower;
mode.ResetD = !smpower;
 
if ( USMC_SetMode ( cur_dev, &mode ) )
return TRUE;
system ( "clear" );
printf ( "Now, Power is %s\n", mode.ResetD ? "Off" : "On" );
 
return FALSE;
}
/8SMC1-USBhF/usmctest/Makefile
0,0 → 1,8
usmctest: usmctest.o libusmc.so
gcc -o usmctest usmctest.o -L. -lusmc -Wl,-rpath,.
 
usmctest.o: usmctest.c
gcc -c usmctest.c
 
clean:
-rm -f *.o