Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

stepper.h

By: Kreldin on Dec 4th, 2012  |  syntax: C++  |  size: 6.73 KB  |  views: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //---------------------------------------------------------------------------
  2. #include "sio_util.h"
  3.  
  4. //--------------------- Stepper Module specific stuff ---------------------------
  5. typedef struct _STEPMOD {
  6.     long                pos;            //current position
  7.     byte                ad;             //a/d value
  8.     unsigned short int  st;             //current step time
  9.     byte                inbyte;         //input bits
  10.     long                home;           //home position
  11.  
  12. //The following data is stored locally for reference
  13.     long                cmdpos;                 //last commanded position
  14.     byte                cmdspeed;               //last commanded speed
  15.     byte                cmdacc;                 //last commanded acceleration
  16.     short int           cmdst;                  //last commanded step time
  17.     double              st_period;
  18.     byte                move_mode;              //last commanded move mode
  19.     byte                min_speed;              //minimum running speed
  20.     byte                stopctrl;               //stop control byte
  21.     byte                outbyte;                //output bits
  22.     byte                homectrl;               //homing control byte
  23.     byte                ctrlmode;               //operating control mode byte
  24.     byte                run_pwm;                //pwm for running current limit
  25.     byte                hold_pwm;               //pwm for holding current limit
  26.     byte                therm_limit;            //thermal limit
  27.     byte                emergency_acc;
  28.     byte                stat_io;                //IO byte, returned in status packet
  29.     } STEPMOD;
  30.  
  31.  
  32. //Step Module Command set:
  33. #define RESET_POS         0x00  //Reset encoder counter to 0 (0 bytes)
  34. #define SET_ADDR          0x01  //Set address and group address (2 bytes)
  35. #define DEF_STAT          0x02  //Define status items to return (1 byte)
  36. #define READ_STAT         0x03  //Read value of current status items
  37. #define LOAD_TRAJ         0x04  //Load trajectory data
  38. #define START_MOVE        0x05  //Start pre-loaded trajectory (0 bytes)
  39. #define SET_PARAM         0x06  //Set operating parameters (6 bytes)
  40. #define STOP_MOTOR        0x07  //Stop motor (1 byte)
  41. #define SET_OUTPUTS       0x08  //Set output bits (1 byte)
  42. #define SET_HOMING        0x09  //Define homing mode (1 byte)
  43. #define SET_BAUD          0x0A  //Set the baud rate (1 byte)
  44. #define RESERVED          0x0B  //
  45. #define SAVE_AS_HOME      0x0C  //Store the input bytes and timer val (0 bytes)
  46. #define NOT_USED          0x0D
  47. #define NOP               0x0E  //No operation - returns prev. defined status (0 bytes)
  48. #define HARD_RESET        0x0F  //RESET - no status is returned
  49.  
  50. //Step Module STATUSITEMS bit definitions:
  51. #define SEND_POS          0x01  //4 bytes data
  52. #define SEND_AD           0x02  //1 byte
  53. #define SEND_ST           0x04  //2 bytes
  54. #define SEND_INBYTE       0x08  //1 byte
  55. #define SEND_HOME         0x10  //4 bytes
  56. #define SEND_ID           0x20  //2 bytes
  57. #define SEND_OUT          0x40  //1 byte
  58.  
  59. //Step Module LOAD_TRAJ control byte bit definitions:
  60. #define LOAD_POS          0x01  //+4 bytes
  61. #define LOAD_SPEED        0x02  //+1 bytes
  62. #define LOAD_ACC          0x04  //+1 bytes
  63. #define LOAD_ST           0x08  //+3 bytes
  64. #define STEP_REV          0x10  //reverse dir
  65. #define START_NOW         0x80  //1 = start now, 0 = wait for START_MOVE command
  66.  
  67. //Step Module SET_PARAM operating mode byte bit definitions:
  68. #define SPEED_8X          0x00  //use 8x timing
  69. #define SPEED_4X          0x01  //use 4x timing
  70. #define SPEED_2X          0x02  //use 2x timing
  71. #define SPEED_1X          0x03  //use 1x timing
  72. #define IGNORE_LIMITS     0x04  //Do not stop automatically on limit switches
  73. #define MOFF_LIMIT        0x08  //Turn Off Motor on Limit
  74. #define MOFF_STOP         0x10  //Turn Off motor on Stop
  75.  
  76. //Step Module STOP_MOTOR control byte bit definitions:
  77. #define STP_ENABLE_AMP    0x01  //1 = raise amp enable output, 0 = lower amp enable
  78. #define STOP_ABRUPT       0x04  //set to stop motor immediately
  79. #define STOP_SMOOTH       0x08  //set to decellerate motor smoothly
  80.  
  81. //Step Module SET_HOMING control byte bit definitions:
  82. #define ON_LIMIT1         0x01  //home on change in limit 1
  83. #define ON_LIMIT2         0x02  //home on change in limit 2
  84. #define HOME_MOTOR_OFF    0x04  //turn motor off when homed
  85. #define ON_HOMESW         0x08  //home on change in index
  86. #define HOME_STOP_ABRUPT  0x10  //stop abruptly when homed
  87. #define HOME_STOP_SMOOTH  0x20  //stop smoothly when homed
  88.  
  89. //Step Module Status byte bit definitions:
  90. #define MOTOR_MOVING      0x01  //Set when motor is moving
  91. #define CKSUM_ERROR       0x02  //checksum error in received command
  92. #define STP_AMP_ENABLED   0x04  //set if amplifier is enabled
  93. #define POWER_ON          0x08  //set when motor power is on
  94. #define AT_SPEED          0x10  //set if the commanded velocity is reached.
  95. #define VEL_MODE          0x20  //set when in velocity profile mode
  96. #define TRAP_MODE         0x40  //set when in trap. profile mode
  97. #define HOME_IN_PROG      0x80  //set while searching for home, cleared when home found
  98.  
  99. //Step Module Input byte bit definitions:
  100. #define ESTOP             0x01  //emergency stop input
  101. #define AUX_IN1           0x02  //auxilliary input #1
  102. #define AUX_IN2           0x04  //auxilliary input #2
  103. #define FWD_LIMIT         0x08  //forward limit switch
  104. #define REV_LIMIT         0x10  //reverse limit switch
  105. #define HOME_SWITCH       0x20  //homing limit switch
  106.  
  107. //Step module function prototypes:
  108. STEPMOD * StepNewMod();
  109.  
  110. DLLENTRY(BOOL) StepGetStat(byte addr);
  111. DLLENTRY(long) StepGetPos(byte addr);
  112. DLLENTRY(byte) StepGetAD(byte addr);
  113. DLLENTRY(unsigned short int) StepGetStepTime(byte addr);
  114. DLLENTRY(byte) StepGetInbyte(byte addr);
  115. DLLENTRY(long) StepGetHome(byte addr);
  116. DLLENTRY(byte) StepGetIObyte(byte addr);
  117. DLLENTRY(byte) StepGetMvMode(byte addr);
  118. DLLENTRY(long) StepGetCmdPos(byte addr);
  119. DLLENTRY(byte) StepGetCmdSpeed(byte addr);
  120. DLLENTRY(byte) StepGetCmdAcc(byte addr);
  121. DLLENTRY(unsigned short int) StepGetCmdST(byte addr);
  122. DLLENTRY(double) StepGetStepPeriod(byte addr);
  123.  
  124. DLLENTRY(byte) StepGetCtrlMode(byte addr);
  125. DLLENTRY(byte) StepGetMinSpeed(byte addr);
  126. DLLENTRY(byte) StepGetRunCurrent(byte addr);
  127. DLLENTRY(byte) StepGetHoldCurrent(byte addr);
  128. DLLENTRY(byte) StepGetThermLimit(byte addr);
  129. DLLENTRY(byte) StepGetEmAcc(byte addr);
  130.  
  131. DLLENTRY(byte) StepGetOutputs(byte addr);
  132. DLLENTRY(byte) StepGetHomeCtrl(byte addr);
  133. DLLENTRY(byte) StepGetStopCtrl(byte addr);
  134.  
  135. DLLENTRY(BOOL) StepSetParam(byte addr, byte mode,
  136.                             byte minspeed, byte runcur, byte holdcur, byte thermlim, byte em_acc = 255);
  137. DLLENTRY(BOOL) StepLoadTraj(byte addr, byte mode, long pos, byte speed, byte acc, unsigned short int steptime);
  138. DLLENTRY(BOOL) StepLoadUnprofiledTraj(byte addr, byte mode, long pos, double step_period);
  139. DLLENTRY(BOOL) StepResetPos(byte addr);
  140. DLLENTRY(BOOL) StepStopMotor(byte addr, byte mode);
  141. DLLENTRY(BOOL) StepSetOutputs(byte addr, byte outbyte);
  142. DLLENTRY(BOOL) StepSetHoming(byte addr, byte mode);
  143.  
  144. DLLENTRY(int)    StepsPerSec2StepTime(double StepsPerSecond, int SpeedFactor);
  145. DLLENTRY(double) StepTime2StepsPerSec(int InitialTimerCount, int SpeedFactor);
  146. DLLENTRY(double) StepsPerSec2mSecPerStep(double StepsPerSecond);
  147. DLLENTRY(double) mSecPerStep2StepsPerSec(double mSecPerStep);
  148.  
  149. DLLENTRY(double) MinStepPeriod(int SpeedFactor);
  150. DLLENTRY(double) MaxStepPeriod(int SpeedFactor);