Skip to content

Commit

Permalink
Merge branch 'master' into feature/celesta-tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquanli authored Dec 7, 2024
2 parents fd12453 + 90a3532 commit 82b3915
Show file tree
Hide file tree
Showing 130 changed files with 21,688 additions and 7,464 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Install Environment and Run Tests

on:
push:
branches: master
pull_request:
jobs:
install-and-test:
permissions:
contents: read
runs-on: ubuntu-22.04
steps:
- name: Checkout the Squid repo
uses: actions/checkout@v4
- name: Run the setup script for Ubuntu 22.04
run: ./setup_22.04.sh
working-directory: ./software
env:
SRC_ROOT: "${{ runner.temp }}"
- name: Run the cuda setup script
run: ./setup_cuda_22.04.sh
working-directory: ./software
env:
SRC_ROOT: "${{ runner.temp }}"
# NOTE(imo): Our setup script checks out the repository, so we actually check it out twice.
# Once to get the script (using actions/checkout@v4), then again via the script. We want to do
# all of our actually testing in the one the script checks out, though, so make sure we set SRC_ROOT
# properly and then use the same working directory for running within the setup script's checkout
- name: "TEMPORARY: copy a valid config into the repo software root"
run: cp configurations/configuration_Squid+.ini .
working-directory: "${{ runner.temp }}/Squid/software"
- name: Run the tests
run: python3 -m pytest
working-directory: "${{ runner.temp }}/Squid/software"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.pyc
*.xml
**/.idea/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Applications include
- BOM for the multicolor laser engine: [link](https://docs.google.com/spreadsheets/d/1hEM6PsxZPTp1LY3cpxUJOS3Q1YLQN-xniF33ZddFj9U/edit#gid=1175873468)
- BOM for the control panel: [link](https://docs.google.com/spreadsheets/d/1z2HjibIG9PHffiDsbuzQXmvf2gSFMduHrXkPwDbcXRY/edit?usp=sharing)

## Eearly Results, Related Work and Possible Applications
## Early Results, Related Work and Possible Applications
Refer to our website: www.squid-imaging.org

## References
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void tmc4361A_init(TMC4361ATypeDef *tmc4361A, uint8_t channel, ConfigurationType
tmc4361A->dac_idx = NO_DAC;
tmc4361A->dac_fullscale_msteps = 0;
tmc4361A->config = config;
tmc4361A->velocity_mode = false;

tmc4361A->config->callback = NULL;
tmc4361A->config->channel = channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ typedef struct
float threadPitch;
uint16_t stepsPerRev;
uint16_t microsteps;
bool velocity_mode;
uint8_t dac_idx;
uint32_t dac_fullscale_msteps;

//TMotorConfig motorConfig;
//TClosedLoopConfig closedLoopConfig;
uint8_t status;
ConfigurationTypeDef *cover;

int target_tolerance;
int pid_tolerance;

} TMC4361ATypeDef;

typedef void (*tmc4361A_callback)(TMC4361ATypeDef*, ConfigState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
tmc4361A_stop: Halt operation by setting the target position to the current position
Arguments: TMC4361ATypeDef *tmc4361A
tmc4361A_isRunning: Returns true if the motor is moving
Arguments: TMC4361ATypeDef *tmc4361A
Arguments: TMC4361ATypeDef *tmc4361A, bool pid_enable
tmc4361A_xmmToMicrosteps: Convert from millimeters to units microsteps for position and jerk values
Arguments: TMC4361ATypeDef *tmc4361A, float mm
tmc4361A_xmicrostepsTomm: Convert from microsteps to units millimeters for position and jerk values
Expand Down Expand Up @@ -434,7 +434,7 @@ void tmc4361A_writeSPR(TMC4361ATypeDef *tmc4361A) {
OPERATION: We write several bytes to the two ICs to configure their behaviors.
ARGUMENTS:
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
uint32_t clk_Hz_TMC4361: Clock frequency we are driving the ICs at
RETURNS: None
Expand All @@ -459,10 +459,10 @@ void tmc4361A_tmc2660_init(TMC4361ATypeDef *tmc4361A, uint32_t clk_Hz_TMC4361) {
// SPI configuration
tmc4361A_writeInt(tmc4361A, TMC4361A_SPIOUT_CONF, 0x4440108A);
// cover datagram for TMC2660
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000900C3);
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000A0000);
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000C000A);
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000E00A0); // SDOFF = 1 -> SPI mode
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000900C3); // CHOPCONF
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000A0000); // SMARTEN
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000C000A); // SGCSCON
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000E00A1); // SDOFF = 1 -> SPI mode
// current scaling
tmc4361A_cScaleInit(tmc4361A);
// microstepping setting
Expand All @@ -471,6 +471,56 @@ void tmc4361A_tmc2660_init(TMC4361ATypeDef *tmc4361A, uint32_t clk_Hz_TMC4361) {
return;
}

/*
-----------------------------------------------------------------------------
DESCRIPTION: tmc4361A_tmc2660_disable_driver() shut off driver MOSFETs
OPERATION: shut off the driver MOSFETs to make axis to disable status
ARGUMENTS:
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
RETURNS: None
LOCAL VARIABLES: None
SHARED VARIABLES:
TMC4361ATypeDef *tmc4361A: Values are read from the struct
GLOBAL VARIABLES: None
DEPENDENCIES: tmc4316A.h
-----------------------------------------------------------------------------
*/
void tmc4361A_tmc2660_disable_driver(TMC4361ATypeDef *tmc4361A) {
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000900C0); // CHOPCONF
}

/*
-----------------------------------------------------------------------------
DESCRIPTION: tmc4361A_tmc2660_enable_driver() enable driver MOSFETs to init argument value
OPERATION: enable the driver MOSFETs to make axis to enable status
ARGUMENTS:
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
RETURNS: None
LOCAL VARIABLES: None
SHARED VARIABLES:
TMC4361ATypeDef *tmc4361A: Values are read from the struct
GLOBAL VARIABLES: None
DEPENDENCIES: tmc4316A.h
-----------------------------------------------------------------------------
*/
void tmc4361A_tmc2660_enable_driver(TMC4361ATypeDef *tmc4361A) {
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, 0x000900C3); // CHOPCONF
}

/*
-----------------------------------------------------------------------------
DESCRIPTION: tmc4361A_tmc2660_update() update the tmc4361A and tmc2660 settings (current scaling and microstepping settings)
Expand Down Expand Up @@ -670,6 +720,7 @@ int8_t tmc4361A_setVirtualLimit(TMC4361ATypeDef *tmc4361A, int dir, int32_t limi
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
uint8_t polarity: Polarity of the switch - 0 if active low, 1 if active high
uint8_t which: Which switch to use as home
uint16_t safety_margin: safty margin of home pointer around
RETURNS: None
Expand All @@ -685,7 +736,7 @@ int8_t tmc4361A_setVirtualLimit(TMC4361ATypeDef *tmc4361A, int dir, int32_t limi
DEPENDENCIES: tmc4316A.h
-----------------------------------------------------------------------------
*/
void tmc4361A_enableHomingLimit(TMC4361ATypeDef *tmc4361A, uint8_t polarity, uint8_t which) {
void tmc4361A_enableHomingLimit(TMC4361ATypeDef *tmc4361A, uint8_t polarity, uint8_t which, uint16_t safety_margin) {
if (which == LEFT_SW) {
if (polarity != 0) {
// If the left switch is active high, HOME_REF = 0 indicates positive direction in reference to X_HOME
Expand All @@ -711,7 +762,7 @@ void tmc4361A_enableHomingLimit(TMC4361ATypeDef *tmc4361A, uint8_t polarity, uin
tmc4361A_setBits(tmc4361A, TMC4361A_REFERENCE_CONF, TMC4361A_STOP_RIGHT_IS_HOME_MASK);
}
// have a safety margin around home
tmc4361A_setBits(tmc4361A, TMC4361A_HOME_SAFETY_MARGIN, 1 << 2);
tmc4361A_setBits(tmc4361A, TMC4361A_HOME_SAFETY_MARGIN, safety_margin);

return;
}
Expand Down Expand Up @@ -932,7 +983,7 @@ void tmc4361A_moveToExtreme(TMC4361ATypeDef *tmc4361A, int32_t vel, int8_t dir)
-----------------------------------------------------------------------------
*/
void tmc4361A_sRampInit(TMC4361ATypeDef *tmc4361A) {
tmc4361A_setBits(tmc4361A, TMC4361A_RAMPMODE, 0b110); // positioning mode, s-shaped ramp
tmc4361A_setBits(tmc4361A, TMC4361A_RAMPMODE, TMC4361A_RAMP_POSITION | TMC4361A_RAMP_SSHAPE); // positioning mode, s-shaped ramp
tmc4361A_rstBits(tmc4361A, TMC4361A_GENERAL_CONF, TMC4361A_USE_ASTART_AND_VSTART_MASK); // keep astart, vstart = 0
tmc4361A_writeInt(tmc4361A, TMC4361A_BOW1, tmc4361A->rampParam[BOW1_IDX]); // determines the value which increases the absolute acceleration value.
tmc4361A_writeInt(tmc4361A, TMC4361A_BOW2, tmc4361A->rampParam[BOW2_IDX]); // determines the value which decreases the absolute acceleration value.
Expand Down Expand Up @@ -1082,8 +1133,9 @@ void tmc4361A_setMaxSpeed(TMC4361ATypeDef *tmc4361A, int32_t velocity) {
-----------------------------------------------------------------------------
*/
void tmc4361A_setSpeed(TMC4361ATypeDef *tmc4361A, int32_t velocity) {
tmc4361A->velocity_mode = true;
tmc4361A_readInt(tmc4361A, TMC4361A_EVENTS); // clear register
tmc4361A_rstBits(tmc4361A, TMC4361A_RAMPMODE, 0b100); // no velocity ramp
tmc4361A_rstBits(tmc4361A, TMC4361A_RAMPMODE, TMC4361A_RAMP_POSITION | TMC4361A_RAMP_HOLD); // no velocity ramp
tmc4361A_writeInt(tmc4361A, TMC4361A_VMAX, velocity);
return;
}
Expand Down Expand Up @@ -1210,12 +1262,16 @@ int8_t tmc4361A_setMaxAcceleration(TMC4361ATypeDef *tmc4361A, uint32_t accelerat
-----------------------------------------------------------------------------
*/
int8_t tmc4361A_moveTo(TMC4361ATypeDef *tmc4361A, int32_t x_pos) {
// ensure we are in positioning mode with S-shaped ramp
tmc4361A_sRampInit(tmc4361A);

if (x_pos < tmc4361A->xmin || x_pos > tmc4361A->xmax) {
return ERR_OUT_OF_RANGE;
}

if(tmc4361A->velocity_mode) {
// ensure we are in positioning mode with S-shaped ramp
tmc4361A_sRampInit(tmc4361A);
tmc4361A->velocity_mode = false;
}

// Read events before and after to clear the register
tmc4361A_readInt(tmc4361A, TMC4361A_EVENTS);
tmc4361A_writeInt(tmc4361A, TMC4361A_X_TARGET, x_pos);
Expand Down Expand Up @@ -1408,6 +1464,7 @@ int8_t tmc4361A_setCurrentPosition(TMC4361ATypeDef *tmc4361A, int32_t position)
tmc4361A_writeInt(tmc4361A, TMC4361A_XACTUAL, position);
tmc4361A_moveTo(tmc4361A, position);

tmc4361A->velocity_mode = true;
return err;
}

Expand Down Expand Up @@ -1447,6 +1504,7 @@ void tmc4361A_stop(TMC4361ATypeDef *tmc4361A) {
ARGUMENTS:
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
bool pid_enable: true: if this aix enable pid control, else: false
RETURNS:
bool moving: true if moving, false otherwise
Expand All @@ -1464,20 +1522,25 @@ void tmc4361A_stop(TMC4361ATypeDef *tmc4361A) {
DEPENDENCIES: tmc4316A.h
-----------------------------------------------------------------------------
*/
bool tmc4361A_isRunning(TMC4361ATypeDef *tmc4361A) {
bool tmc4361A_isRunning(TMC4361ATypeDef *tmc4361A, bool pid_enable) {
int32_t stat_reg = tmc4361A_readInt(tmc4361A, TMC4361A_STATUS);

// We aren't running if target is reached OR (velocity = 0 and acceleration == 0)
if ((stat_reg & TMC4361A_TARGET_REACHED_MASK) != 0) {
return true;
if (pid_enable) {
int32_t pid_err = abs(tmc4361A_readInt(tmc4361A, TMC4361A_PID_E_RD));
// We aren't running if target is reached OR (velocity = 0 and acceleration == 0)
if (((stat_reg & TMC4361A_TARGET_REACHED_MASK) == 1) && ((stat_reg & (TMC4361A_VEL_STATE_F_MASK | TMC4361A_RAMP_STATE_F_MASK))==0) && (pid_err < tmc4361A->target_tolerance)) {
return false;
}
}
stat_reg &= (TMC4361A_VEL_STATE_F_MASK | TMC4361A_RAMP_STATE_F_MASK);
if (stat_reg == 0) {
return true;
else {
// We aren't running if target is reached OR (velocity = 0 and acceleration == 0)
if (((stat_reg & TMC4361A_TARGET_REACHED_MASK) == 1) && ((stat_reg & (TMC4361A_VEL_STATE_F_MASK | TMC4361A_RAMP_STATE_F_MASK))==0)) {
return false;
}
}

// Otherwise, return false
return false;
// Otherwise, return true
return true;
}

/*
Expand Down Expand Up @@ -1776,15 +1839,18 @@ void tmc4361A_init_PID(TMC4361ATypeDef *tmc4361A, uint32_t target_tolerance, uin

// Write the PID parameters
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_P_WR, pid_p & TMC4361A_PID_P_MASK);
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_I_WR, pid_d & TMC4361A_PID_I_MASK);
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_D_WR, pid_p & TMC4361A_PID_D_MASK);
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_I_WR, pid_i & TMC4361A_PID_I_MASK);
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_D_WR, pid_d & TMC4361A_PID_D_MASK);

tmc4361A_writeInt(tmc4361A, TMC4361A_PID_DV_CLIP_WR, pid_dclip & TMC4361A_PID_DV_CLIP_MASK);

// Set up the datagram
datagram = ((pid_iclip << TMC4361A_PID_I_CLIP_SHIFT) & TMC4361A_PID_I_CLIP_MASK) + ((pid_d_clkdiv << TMC4361A_PID_D_CLKDIV_SHIFT) & TMC4361A_PID_D_CLKDIV_MASK);
tmc4361A_writeInt(tmc4361A, TMC4361A_PID_I_CLIP_WR, datagram);

// save paramters
tmc4361A->target_tolerance = target_tolerance;
tmc4361A->pid_tolerance = pid_tolerance;
return;
}

Expand Down Expand Up @@ -2002,3 +2068,61 @@ int8_t tmc4361A_move_no_stick(TMC4361ATypeDef *tmc4361A, int32_t x_pos, int32_t

return tmc4361A_moveTo_no_stick(tmc4361A, target, backup_amount, err_thresh, timeout_ms);
}

/*
-----------------------------------------------------------------------------
DESCRIPTION: tmc4361A_config_init_stallGuard() initializes stall prevention on the TMC4316A and TMC2660
OPERATION: First, check if arugments are within bounds. If the argument exceed the bounds, constrain them before writing the values, and note that this function failed.
We then write the sensitivitity to the TMC2660.
ARGUMENTS:
TMC4361ATypeDef *tmc4361A: Pointer to a struct containing motor driver info
int8_t sensitivity: Value from -64 to +63 indicating sensitivity to stall condition. Larger values are less sensitive.
bool filter_en: Set true to use filter (more accurate, slower).
uint32_t vstall_lim: 24-bit value. The internal ramp velocity is set immediately to 0 whenever a stall is detected and |VACTUAL| >VSTALL_LIMIT.
RETURNS:
bool success: return true if there were no errors
INPUTS / OUTPUTS: Sends signals over SPI
LOCAL VARIABLES: None
SHARED VARIABLES:
TMC4361ATypeDef *tmc4361A: Values are read from the struct
GLOBAL VARIABLES: None
DEPENDENCIES: tmc4316A.h
-----------------------------------------------------------------------------
*/
int16_t tmc4361A_config_init_stallGuard(TMC4361ATypeDef *tmc4361A, int8_t sensitivity, bool filter_en, uint32_t vstall_lim){
// First, ensure values are within limits
bool success = true;
if((sensitivity > 63) || (sensitivity < -64) || (vstall_lim >= (1<<24))){
success = false;
}
sensitivity = constrain(sensitivity, -64, 63);
vstall_lim = constrain(vstall_lim, 0, ((1<<24)-1));
// Mask the high bit
sensitivity = sensitivity & 0x7F;
// Build the datagram
uint32_t datagram = 0;
datagram = filter_en ? SFILT : 0;
datagram |= SGCSCONF;
datagram |= (sensitivity << 8);
datagram |= tmc4361A->cscaleParam[CSCALE_IDX];
// Next, write to the TMC2660 - write to the "cover_0.3 *10^6 /(200*256)low" register
tmc4361A_writeInt(tmc4361A, TMC4361A_COVER_LOW_WR, datagram);
// Enable stall detection on the TMC4316A
// set vstall limit
tmc4361A_writeInt(tmc4361A, TMC4361A_VSTALL_LIMIT_WR, vstall_lim);
// enable stop on stall
tmc4361A_setBits(tmc4361A, TMC4361A_REFERENCE_CONF, TMC4361A_STOP_ON_STALL_MASK);
// disable drive after stall
tmc4361A_rstBits(tmc4361A, TMC4361A_REFERENCE_CONF, TMC4361A_DRV_AFTER_STALL_MASK);

return success;
}
Loading

0 comments on commit 82b3915

Please sign in to comment.