Haptic Controller
Loading...
Searching...
No Matches
main.cpp File Reference

Go to the source code of this file.

Classes

struct  SystemInfo
 Struct to hold important system information. More...
 

Functions

void onOdriveCanMessage (const CanMsg &msg)
 
void endEffMirrorUpdate ()
 : Links end effector positions together
 
void jointMirrorUpdate ()
 : Links joints states
 
void enterState (ControllerState state)
 Process enter state requests.
 
void exitState (ControllerState state)
 Process exit state requests.
 
void printEverything ()
 Print statements, set at a separate frequency than others.
 
bool shouldError ()
 Check if there is a critical error that should prompt a transition to the error state.
 
void doReboot ()
 Reboot the Teensy.
 
void openRelays ()
 Open relays to disable motors.
 
void closeRelays ()
 Close relays to enable motors.
 
void request_disable ()
 Request estop from odrives.
 
void request_enable ()
 Process enable request, clear errors.
 
bool isInitialized ()
 Check if the motors are in the initial position.
 
bool isSynced ()
 Check if the motors are in sync.
 
void ledUpdate ()
 Update the LED strip and on board LED based on the current state, timeouts, and mirror states.
 
void doState ()
 Handles the current state of the controller.
 
void changeState ()
 Evaluates state transitions based on current state and system information.
 
void updateSystemInfo ()
 Update system information struct with new CAN information.
 
void updateAndSendSystemInfo ()
 Send the end effector position over CAN to the other teensy.
 
void checkDeadman ()
 Check the deadman switch state.
 
void setup ()
 setup function before main loop
 
void loop ()
 Main loop.
 

Variables

VirtualTimerGroup timer_group {}
 
VirtualTimerGroup micro_timer_group {}
 
TeensyCAN< 1 > teensy_can
 
uint32_t last_received_time
 
FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_256 > odrive_can0
 
FlexCAN_T4< CAN3, RX_SIZE_256, TX_SIZE_256 > odrive_can1
 
ODriveUserData odrv0_user_data {}
 
ODriveUserData odrv1_user_data {}
 
OdriveController< CAN2 > odrv0 = OdriveController<CAN2>(odrive_can0, ODRIVE0_ID, odrv0_user_data)
 : Odrive for first joint
 
OdriveController< CAN3 > odrv1 = OdriveController<CAN3>(odrive_can1, ODRIVE1_ID, odrv1_user_data)
 : Odrive for second joint
 
CommsController< ODRV_CAN0, ODRV_CAN1 > comms_controller
 
LowPassFilter filter = LowPassFilter(20.0)
 
MovingAverageFilter filter_phi_0 {1}
 
MovingAverageFilter filter_phi_1 {1}
 
MovingAverageFilter filter_phi_dot_0 {1}
 
MovingAverageFilter filter_phi_dot_1 {1}
 
std::vector< float > joint_thetas = {0.0f, 0.0f}
 
std::vector< float > joint_vels = {0.0f, 0.0f}
 
std::vector< float > pos = {0.0f, 0.0f}
 
std::vector< float > vel = {0.0f, 0.0f}
 
SystemInfo sys_inf = SystemInfo(comms_controller)
 
ControllerState controller_state = ControllerState::RUNNING
 

Function Documentation

◆ changeState()

void changeState ( )

Evaluates state transitions based on current state and system information.

Definition at line 361 of file main.cpp.

References comms_controller, controller_state, SystemInfo::deadman_switch_pressed, enterState(), ERROR, exitState(), INITIALIZING, isInitialized(), isSynced(), SystemInfo::mirror_teensy_state, printEverything(), READY, RUNNING, shouldError(), and sys_inf.

Referenced by setup().

◆ checkDeadman()

void checkDeadman ( )

Check the deadman switch state.

Definition at line 668 of file main.cpp.

References comms_controller, SystemInfo::deadman_switch_pressed, and sys_inf.

Referenced by setup().

◆ closeRelays()

void closeRelays ( )

Close relays to enable motors.

Definition at line 195 of file main.cpp.

Referenced by exitState().

◆ doReboot()

void doReboot ( )

Reboot the Teensy.

Definition at line 177 of file main.cpp.

Referenced by enterState().

◆ doState()

void doState ( )

◆ endEffMirrorUpdate()

void endEffMirrorUpdate ( )

: Links end effector positions together

Note
: Used for robotic and haptic fingers

Definition at line 599 of file main.cpp.

References comms_controller, joint_thetas, limit(), pos, soft_limit_torques(), spatial_jacobian(), tau_to_torque(), and vel.

Referenced by doState().

◆ enterState()

void enterState ( ControllerState state)

Process enter state requests.

Parameters
state- The state to enter

Definition at line 448 of file main.cpp.

References comms_controller, doReboot(), ERROR, INITIALIZING, SystemInfo::odrive0_heartbeat_timeout, SystemInfo::odrive1_heartbeat_timeout, READY, request_disable(), request_enable(), RUNNING, sys_inf, and SystemInfo::teensy_heartbeat_timeout.

Referenced by changeState(), and setup().

◆ exitState()

void exitState ( ControllerState state)

Process exit state requests.

Parameters
state- The state to exit

Definition at line 474 of file main.cpp.

References closeRelays(), comms_controller, ERROR, INITIALIZING, READY, and RUNNING.

Referenced by changeState(), and setup().

◆ isInitialized()

bool isInitialized ( )

Check if the motors are in the initial position.

Returns
true if motors are in initial position
false if motors are not in initial position

Definition at line 226 of file main.cpp.

Referenced by changeState().

◆ isSynced()

bool isSynced ( )

Check if the motors are in sync.

Returns
true if motors are in sync
false if motors are not in sync

Definition at line 239 of file main.cpp.

References comms_controller, float_close_compare(), joint_thetas, and pos.

Referenced by changeState().

◆ jointMirrorUpdate()

void jointMirrorUpdate ( )

: Links joints states

Note
: Used for robotic and haptic thumb

Definition at line 562 of file main.cpp.

References comms_controller, joint_thetas, joint_vels, limit(), soft_limit_torques(), and tau_to_torque().

Referenced by doState().

◆ ledUpdate()

void ledUpdate ( )

Update the LED strip and on board LED based on the current state, timeouts, and mirror states.

Definition at line 260 of file main.cpp.

References blue, controller_state, cyan, ERROR, green, INITIALIZING, last_received_time, magneta, SystemInfo::odrive0_heartbeat_timeout, SystemInfo::odrive1_heartbeat_timeout, READY, red, RUNNING, setColor(), sys_inf, SystemInfo::teensy_heartbeat_timeout, and yellow.

Referenced by setup().

◆ loop()

void loop ( )

Main loop.

Definition at line 749 of file main.cpp.

References comms_controller, micro_timer_group, teensy_can, and timer_group.

◆ onOdriveCanMessage()

void onOdriveCanMessage ( const CanMsg & msg)

Definition at line 49 of file main.cpp.

References odrv0, odrv1, and OdriveController< CAN >::odrv_.

◆ openRelays()

void openRelays ( )

Open relays to disable motors.

Definition at line 186 of file main.cpp.

Referenced by doState().

◆ printEverything()

void printEverything ( )

Print statements, set at a separate frequency than others.

Definition at line 110 of file main.cpp.

References comms_controller, joint_thetas, joint_vels, pos, and vel.

Referenced by changeState(), and setup().

◆ request_disable()

void request_disable ( )

Request estop from odrives.

Definition at line 204 of file main.cpp.

References comms_controller.

Referenced by enterState().

◆ request_enable()

void request_enable ( )

Process enable request, clear errors.

Definition at line 212 of file main.cpp.

References clear_safe_errors(), and comms_controller.

Referenced by enterState().

◆ setup()

◆ shouldError()

bool shouldError ( )

Check if there is a critical error that should prompt a transition to the error state.

Returns
true if there is a critical error
false if there is no critical error to prompt transition to error state

Definition at line 153 of file main.cpp.

References clear_safe_errors(), comms_controller, SystemInfo::estop_enabled, ODriveUserData::last_heartbeat, SystemInfo::odrive0_heartbeat_timeout, SystemInfo::odrive1_heartbeat_timeout, odrv0_user_data, odrv1_user_data, SystemInfo::relays_open, sys_inf, and SystemInfo::teensy_heartbeat_timeout.

Referenced by changeState().

◆ updateAndSendSystemInfo()

◆ updateSystemInfo()

void updateSystemInfo ( )

Update system information struct with new CAN information.

Definition at line 493 of file main.cpp.

References comms_controller, SystemInfo::mirror_teensy_state, sys_inf, and SystemInfo::teensy_heartbeat_timeout.

Referenced by setup().

Variable Documentation

◆ comms_controller

CommsController<ODRV_CAN0, ODRV_CAN1> comms_controller
Initial value:
TeensyCAN< 1 > teensy_can
Definition main.cpp:29
VirtualTimerGroup micro_timer_group
Definition main.cpp:28
OdriveController< CAN3 > odrv1
: Odrive for second joint
Definition main.cpp:46
FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_256 > odrive_can0
Definition main.cpp:36
FlexCAN_T4< CAN3, RX_SIZE_256, TX_SIZE_256 > odrive_can1
Definition main.cpp:37
OdriveController< CAN2 > odrv0
: Odrive for first joint
Definition main.cpp:43
void onOdriveCanMessage(const CanMsg &msg)
Definition main.cpp:49

Definition at line 55 of file main.cpp.

Referenced by changeState(), checkDeadman(), endEffMirrorUpdate(), enterState(), exitState(), isSynced(), jointMirrorUpdate(), loop(), printEverything(), request_disable(), request_enable(), setup(), shouldError(), updateAndSendSystemInfo(), and updateSystemInfo().

◆ controller_state

Definition at line 171 of file main.cpp.

Referenced by changeState(), doState(), ledUpdate(), and setup().

◆ filter

LowPassFilter filter = LowPassFilter(20.0)

Definition at line 63 of file main.cpp.

◆ filter_phi_0

MovingAverageFilter filter_phi_0 {1}

Definition at line 65 of file main.cpp.

Referenced by updateAndSendSystemInfo().

◆ filter_phi_1

MovingAverageFilter filter_phi_1 {1}

Definition at line 66 of file main.cpp.

Referenced by updateAndSendSystemInfo().

◆ filter_phi_dot_0

MovingAverageFilter filter_phi_dot_0 {1}

Definition at line 67 of file main.cpp.

Referenced by updateAndSendSystemInfo().

◆ filter_phi_dot_1

MovingAverageFilter filter_phi_dot_1 {1}

Definition at line 68 of file main.cpp.

Referenced by updateAndSendSystemInfo().

◆ joint_thetas

std::vector<float> joint_thetas = {0.0f, 0.0f}

◆ joint_vels

std::vector<float> joint_vels = {0.0f, 0.0f}

Definition at line 71 of file main.cpp.

Referenced by jointMirrorUpdate(), printEverything(), and updateAndSendSystemInfo().

◆ last_received_time

uint32_t last_received_time

Definition at line 33 of file main.cpp.

Referenced by ledUpdate().

◆ micro_timer_group

VirtualTimerGroup micro_timer_group {}

Definition at line 28 of file main.cpp.

Referenced by loop(), and setup().

◆ odrive_can0

FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_256> odrive_can0

Definition at line 36 of file main.cpp.

◆ odrive_can1

FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_256> odrive_can1

Definition at line 37 of file main.cpp.

◆ odrv0

OdriveController<CAN2> odrv0 = OdriveController<CAN2>(odrive_can0, ODRIVE0_ID, odrv0_user_data)

: Odrive for first joint

Definition at line 43 of file main.cpp.

Referenced by onOdriveCanMessage().

◆ odrv0_user_data

ODriveUserData odrv0_user_data {}

Definition at line 39 of file main.cpp.

Referenced by shouldError(), and updateAndSendSystemInfo().

◆ odrv1

OdriveController<CAN3> odrv1 = OdriveController<CAN3>(odrive_can1, ODRIVE1_ID, odrv1_user_data)

: Odrive for second joint

Definition at line 46 of file main.cpp.

Referenced by onOdriveCanMessage().

◆ odrv1_user_data

ODriveUserData odrv1_user_data {}

Definition at line 40 of file main.cpp.

Referenced by shouldError(), and updateAndSendSystemInfo().

◆ pos

std::vector<float> pos = {0.0f, 0.0f}

◆ sys_inf

◆ teensy_can

TeensyCAN<1> teensy_can

Definition at line 29 of file main.cpp.

Referenced by loop(), and setup().

◆ timer_group

VirtualTimerGroup timer_group {}

Definition at line 27 of file main.cpp.

Referenced by loop(), and setup().

◆ vel

std::vector<float> vel = {0.0f, 0.0f}

Definition at line 73 of file main.cpp.

Referenced by endEffMirrorUpdate(), printEverything(), and updateAndSendSystemInfo().