Bioscara
DALSA's DIY SCARA Robot Arm.
Loading...
Searching...
No Matches
Classes | Enumerations | Functions | Variables
bioscara_joint_firmware Namespace Reference

Joint firmware. More...

Classes

class  Lowpass
 Simple discrete IIR lowpass filter. More...
 
class  MovMax
 Simple FIR moving maximum filter. More...
 

Enumerations

enum  stp_reg_t {
  PING = 0x0f , SETUP = 0x10 , SETRPM = 0x11 , GETDRIVERRPM = 0x12 ,
  MOVESTEPS = 0x13 , MOVEANGLE = 0x14 , MOVETOANGLE = 0x15 , GETMOTORSTATE = 0x16 ,
  RUNCOTINOUS = 0x17 , ANGLEMOVED = 0x18 , SETCURRENT = 0x19 , SETHOLDCURRENT = 0x1A ,
  SETMAXACCELERATION = 0x1B , SETMAXDECELERATION = 0x1C , SETMAXVELOCITY = 0x1D , ENABLESTALLGUARD = 0x1E ,
  DISABLESTALLGUARD = 0x1F , CLEARSTALL = 0x20 , SETBRAKEMODE = 0x22 , ENABLEPID = 0x23 ,
  DISABLEPID = 0x24 , ENABLECLOSEDLOOP = 0x25 , DISABLECLOSEDLOOP = 0x26 , SETCONTROLTHRESHOLD = 0x27 ,
  MOVETOEND = 0x28 , STOP = 0x29 , GETPIDERROR = 0x2A , CHECKORIENTATION = 0x2B ,
  GETENCODERRPM = 0x2C , HOME = 0x2D , HOMEOFFSET = 0x2E
}
 register and command definitions More...
 

Functions

template<typename T >
void readValue (T &val, uint8_t *rxBuf, size_t rx_length)
 Reads a value from a buffer to a value of the specified type.
 
template<typename T >
int writeValue (const T val, uint8_t *txBuf, size_t &tx_length)
 Writes a value of the specified type to a buffer.
 
void blocking_handler (uint8_t reg)
 Handles commands received via I2C.
 
void non_blocking_handler (uint8_t reg)
 Handles read request received via I2C.
 
void set_flags_for_blocking_handler (uint8_t reg)
 prepare flags to initiate the blocking handling of the received comman.
 
void receiveEvent (int n)
 I2C receive event Handler.
 
void requestEvent ()
 I2C request event Handler.
 
void setup (void)
 Setup Peripherals.
 
void loop (void)
 Main loop.
 
float stall_threshold (float qd_rad, float offset)
 computes the speed adaptive threshold.
 

Variables

UstepperS32 stepper
 The core UstepperS32 stepper object to control the motor.
 
uint8_t reg = 0
 
uint8_t blk_reg = 0
 
uint8_t rx_buf [MAX_BUFFER] = {0}
 
uint8_t tx_buf [MAX_BUFFER+RFLAGS_SIZE] = {0}
 
bool rx_data_ready = 0
 
size_t tx_length = 0
 
size_t rx_length = 0
 

Detailed Description

Joint firmware.

The joint firmware is implemented in the Arduino framework. Initially the setup() function is called and subsequently the loop() function in an infinite loop. The receiveEvent() is executed when a I2C message is received, and the requestEvent() when data is requested. This always happens sequentially because at least always the #state flags are returned to the robot controller

Enumeration Type Documentation

◆ stp_reg_t

register and command definitions

a register can be read (R) or written (W), each register has a size in bytes. The payload can be split into multiple values or just be a single value. Note that not all functions are implemented.

Enumerator
PING 

R; Size: 1; [(char) ACK].

SETUP 

W; Size: 2; [(uint8) holdCurrent, (uint8) driveCurrent].

SETRPM 

W; Size: 4; [(float) RPM].

GETDRIVERRPM 
MOVESTEPS 

W; Size: 4; [(int32) steps].

MOVEANGLE 
MOVETOANGLE 

W; Size: 4; [(float) degrees].

GETMOTORSTATE 
RUNCOTINOUS 
ANGLEMOVED 

R; Size: 4; [(float) degrees].

SETCURRENT 

W; Size: 1; [(uint8) driveCurrent].

SETHOLDCURRENT 

W; Size: 1; [(uint8) holdCurrent].

SETMAXACCELERATION 

W; Size: 4; [(float) deg/s^2].

SETMAXDECELERATION 
SETMAXVELOCITY 

W; Size: 4; [(float) deg/s].

ENABLESTALLGUARD 

W; Size: 1; [(uint8) threshold].

DISABLESTALLGUARD 
CLEARSTALL 
SETBRAKEMODE 

W; Size: 1; [(uint8) mode].

ENABLEPID 
DISABLEPID 
ENABLECLOSEDLOOP 
DISABLECLOSEDLOOP 

W; Size: 1; [(uint8) 0].

SETCONTROLTHRESHOLD 
MOVETOEND 
STOP 

W; Size: 1; [(uint8) mode].

GETPIDERROR 
CHECKORIENTATION 

W; Size: 4; [(float) degrees].

GETENCODERRPM 

R; Size: 4; [(float) RPM].

HOME 

W; Size: 4; [(uint8) current, (uint8) sensitivity, (uint8) speed, (uint8) direction].

HOMEOFFSET 

R/W; Size: 4; [(float) -].

Function Documentation

◆ blocking_handler()

void bioscara_joint_firmware::blocking_handler ( uint8_t  reg)

Handles commands received via I2C.

Warning
This is a blocking function which may take some time to execute. This function must not be called from an ISR or callback! Call from main loop instead.

The registers handled in this handler are those whose implementation can take time and can thereby not be called directly from the request handler.

Parameters
regcommand that should be executed.

◆ loop()

void bioscara_joint_firmware::loop ( void  )

Main loop.

Executes the following:

  1. if isStallguardEnabled: compares stepper.getPidError() with stallguardThreshold and sets isStalled flag.
  2. if rx_data_ready: set isBusy flag to indicate device is busy. Invoke blocking_handler. Clear isBusy flag to indicate device is no longer busy

◆ non_blocking_handler()

void bioscara_joint_firmware::non_blocking_handler ( uint8_t  reg)

Handles read request received via I2C.

Can be invoked from the I2C ISR since reads from the stepper are non-blocking. Also Handling reads and the subsequent wire.write(), did not work from the main loop.

Parameters
regcommand to execute/register to read.

◆ readValue()

template<typename T >
void bioscara_joint_firmware::readValue ( T &  val,
uint8_t *  rxBuf,
size_t  rx_length 
)

Reads a value from a buffer to a value of the specified type.

Parameters
valReference to output variable
rxBufBuffer to read value from
rx_lengthLength of the buffer

◆ receiveEvent()

void bioscara_joint_firmware::receiveEvent ( int  n)

I2C receive event Handler.

Reads the content of the received message. Saves the register so it can be used in the main loop. If the master invokes the read() function the message contains only the register byte and no payload. If the master invokes the write() the message has a payload of appropriate size for the command. Every I2C transaction starts with a receive event when the command is sent and is immediatly followed by a request since at minimum the flags need to be transmitted back. This means that the receive handler and request handler are always executed sequentially. The main loop is not executed since both handlers are ISRs. For a read request the message looks like this:
< [REG]
> [TXBUFn]...[TXBUF2][TXBUF1][TXBUF0][FLAGS]
For a command the message looks like this:
< [REG][RXBUFn]...[RXBUF2][RXBUF1][RXBUF0]
> [FLAGS]
The payload is read into the rx_buf, rx_length is set to the payload length.

Parameters
nthe number of bytes read from the controller device: MAX_BUFFER

◆ requestEvent()

void bioscara_joint_firmware::requestEvent ( )

I2C request event Handler.

Sends the response data to the master. Every transaction begins with a receive event. The request event is always triggered since at a minimum the status flags are returned to the master. Hence this function is only invoked after the receiveEvent() handler has been called. The function calls the non_blocking_handler() which is non-blocking. Since most Ustepper functions are non-blocking as they just read/write registers to the stepper driver/encoder they can be handled directly in the ISR. The non_blocking_handler() populates the tx_buf with relevant data, the current state flags are appended to the tx_buf and then it is send to the master.

◆ set_flags_for_blocking_handler()

void bioscara_joint_firmware::set_flags_for_blocking_handler ( uint8_t  reg)

prepare flags to initiate the blocking handling of the received comman.

Sets the blk_reg to reg, this makes sure that even if a new command is received the blocking hanlder access the latest blocking command. Sets the isBusy flag, to indicate immediatly that the blocking has not been finished. This is if a status request follows immidiatly the command, before the context back to the main loop has happened. Sets the rx_data_ready flag to indicate to the main loop that there is data to read in the blocking handler. Sets the tx_length to 0 because blocking commands can not return a payload.

◆ setup()

void bioscara_joint_firmware::setup ( void  )

Setup Peripherals.

Setup I2C with the address ADR, and begin Serial for debugging with baudrate 9600. Setup the stepper, perform orientation check to check wiring and disable the stepper again.

◆ stall_threshold()

float bioscara_joint_firmware::stall_threshold ( float  qd_rad,
float  offset 
)

computes the speed adaptive threshold.

Parameters
qd_radspeed in rad/s. Should be measured speed because set speed is only available in velocity mode.

◆ writeValue()

template<typename T >
int bioscara_joint_firmware::writeValue ( const T  val,
uint8_t *  txBuf,
size_t &  tx_length 
)

Writes a value of the specified type to a buffer.

Parameters
valReference to input variable
txBufpointer to tx buffer
tx_lengthLength of the buffer returne
Returns
0 On success

Variable Documentation

◆ blk_reg

uint8_t bioscara_joint_firmware::blk_reg = 0

◆ reg

uint8_t bioscara_joint_firmware::reg = 0

◆ rx_buf

uint8_t bioscara_joint_firmware::rx_buf[MAX_BUFFER] = {0}

◆ rx_data_ready

bool bioscara_joint_firmware::rx_data_ready = 0

◆ rx_length

size_t bioscara_joint_firmware::rx_length = 0

◆ stepper

UstepperS32 bioscara_joint_firmware::stepper

The core UstepperS32 stepper object to control the motor.

◆ tx_buf

uint8_t bioscara_joint_firmware::tx_buf[MAX_BUFFER+RFLAGS_SIZE] = {0}

◆ tx_length

size_t bioscara_joint_firmware::tx_length = 0