Anti-Sway Capstone 1.0
Loading...
Searching...
No Matches
io.c File Reference

Sensor/Actuator (Input/Output) Interfacing Library. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <pthread.h>
#include "MyRio.h"
#include "DIO.h"
#include "T1.h"
#include "conC_Encoder_initialize.h"
#include "discrete-lib.h"
#include "error.h"
#include "thread-lib.h"
#include "io.h"
Include dependency graph for io.c:

Macros

#define X_CONNECTOR_ID   0
 X Motor Encoder Connector ID (on Connector C)
 
#define Y_CONNECTOR_ID   1
 Y Motor Encoder Connector ID (on Connector C)
 
#define POTENTIOMETER_SLOPE   -2.11 * PI / 180.0
 
#define POT_V_LIM_LO   -20.0
 Lower Potentiometer Voltage Saturation Limit (V)
 
#define POT_V_LIM_HI   20.0
 Upper Potentiometer Voltage Saturation Limit (V)
 
#define ENC_CNT_REV   2000.0
 
#define M_PER_REV   0.01267 * PI
 
#define ENC_2_POS(value)    (value) / ENC_CNT_REV * M_PER_REV
 
#define ENC_2_VEL(value)    (value) / (BTI_S * ENC_CNT_REV) * M_PER_REV
 
#define X_LIM_LO   0.0
 Lower X Limit.
 
#define Y_LIM_LO   0.0
 Lower Y Limit.
 
#define X_LIM_HI   0.35
 Higher X Limit.
 
#define Y_LIM_HI   0.35
 Higher Y Limit.
 
#define VEL_LIM_ABS   1.0
 Absolute Velocity Limit.
 
#define CHANNELS   16
 Number of Channels.
 
#define LCD_KEYPAD_LEN   4
 Keypad Length.
 
#define UNIT_VEL   0.15
 
#define DEL_ROW   7
 
#define DEL_COL   3
 
#define WAIT_CONST   417000
 

Typedefs

typedef bool Keymap[9]
 

Functions

static void * KeymapThread (void *resource)
 
static int HandleEncoderError (Positions *curr_pos, Velocities *curr_vel)
 
static int HandlePotentiometerError (Angles *curr_ang)
 
static void wait ()
 
int IOSetup ()
 
int IOShutdown ()
 
void Reset ()
 
int GetReferenceVelocityCommand (Velocities *result)
 
int GetReferenceAngleCommand (Angles *result)
 
int GetAngle (Angles *result)
 
int GetTrolleyPosition (Positions *result)
 
int GetTrolleyVelocity (Velocities *result)
 
int GetUserPosition (Angles *angle, Positions *pos, Positions *result)
 
int GetUserVelocity (Angles *angle, Velocities *vel, Velocities *result)
 
int SetXVoltage (Voltage voltage)
 
int SetYVoltage (Voltage voltage)
 
bool PressedDelete ()
 
int KeyboardControlFork ()
 
int KeyboardControlJoin ()
 
char getkey ()
 

Variables

static bool reset
 Reset Variable for measuring velocity.
 
static float potentiometer_v_x_intercept
 
static float potentiometer_v_y_intercept
 
static MyRio_Aio x_potentiometer
 X Potentiometer.
 
static MyRio_Aio y_potentiometer
 Y Potentiometer.
 
static MyRio_Encoder x_encoder
 X Motor Encoder.
 
static MyRio_Encoder y_encoder
 Y Motor Encoder.
 
static int32_t first_enc_state [2]
 
static int32_t prev_enc_state [2]
 
static bool holding_vel_set
 Indicator if the holding for velocity is set.
 
static bool holding_pos_set
 Indicator if the holding for position is set.
 
static Velocities holding_vel
 Encoder Holding for velocity.
 
static Positions holding_pos
 Encoder Holding for position.
 
static const Encoder_StatusMask enc_st_mask
 Encoder Error Mask.
 
MyRio_Aio x_motor
 X Motor Voltage Channel.
 
MyRio_Aio y_motor
 Y Motor Voltage Channel.
 
MyRio_IrqTimer timer
 Universal Timer.
 
static MyRio_Dio channel [CHANNELS]
 Keyboard channels.
 
static pthread_mutex_t keyboard
 Keyboard lock.
 
static Keymap keymap
 Our keymap.
 
static pthread_t keymap_thread
 Thread for Keymap Thread.
 
static ThreadResource keymap_resource
 Thread Resource for Keymap Thread.
 
static int error
 Local Error Flag.
 

Detailed Description

Sensor/Actuator (Input/Output) Interfacing Library.

Author
Anti-Sway Team: Nguyen, Tri; Espinola, Malachi; Tevy, Vattanary; Hokenstad, Ethan; Neff, Callen)
Version
0.1
Date
2024-06-03

Macro Definition Documentation

◆ ENC_2_POS

#define ENC_2_POS ( value)     (value) / ENC_CNT_REV * M_PER_REV

Converts a BDI quantity to meters

Parameters
valueTHe BDI to convert

◆ ENC_2_VEL

#define ENC_2_VEL ( value)     (value) / (BTI_S * ENC_CNT_REV) * M_PER_REV

Converts a BDI/BTI quantity to meters per second

Parameters
valueThe value to convert

◆ ENC_CNT_REV

#define ENC_CNT_REV   2000.0

Number of counts in one revolution TODO(nguy8tri): Find this quantity

◆ M_PER_REV

#define M_PER_REV   0.01267 * PI

Meters per revolution Diameter of upper pulley (12 mm) * PI

◆ POTENTIOMETER_SLOPE

#define POTENTIOMETER_SLOPE   -2.11 * PI / 180.0

Best-Fit Potentiometer Slope (rad/V) TODO(nguy8tri): Find this quantity

◆ UNIT_VEL

#define UNIT_VEL   0.15

The unit velocity stop corresponding to a keypad touch (m/s)

Typedef Documentation

◆ Keymap

typedef bool Keymap[9]

Holds booleans indicating which buttons (1 through 9) are being pressed

Function Documentation

◆ GetAngle()

int GetAngle ( Angles * result)

Obtains the angle of the harness

Parameters
resultA return parameter, which will become the angle along both directions
Returns
0 upon success, other integers if otherwise
result, which will define the angle of the harness along both lateral directions
Here is the call graph for this function:

◆ getkey()

char getkey ( )

Keypad characters

Locking style allows for getkey() to take precedence over all other keyboard commands

Here is the call graph for this function:

◆ GetReferenceAngleCommand()

int GetReferenceAngleCommand ( Angles * result)

Obtains the user command (for tracking)

Parameters
resultA return parameter, which will become the desired angle requested by the user
Returns
0 upon success, negative otherwise
An Angles structure, which reflects the angle requested from the user

◆ GetReferenceVelocityCommand()

int GetReferenceVelocityCommand ( Velocities * result)

Obtains the user command (for anti-sway)

Parameters
resultA return parameter, which will become the change in position requested by the user
Returns
0 upon success, negative otherwise
A Velocities structure, which reflects the change in position requested from the user

Setup discrete velocity commands, -1, 0, and 1

◆ GetTrolleyPosition()

int GetTrolleyPosition ( Positions * result)

Obtains the Trolley Position

Parameters
resultA return parameter, which will become the position of the trolley
Returns
0 upon success, other integers if otherwise
A Positions structure, which defines the Position of the Motor in the lateral plane
Precondition
This is called precisely once every BTI
Here is the call graph for this function:

◆ GetTrolleyVelocity()

int GetTrolleyVelocity ( Velocities * result)

Obtains the Trolley Velocity

Parameters
resultA return parameter, which will become the velocity of the trolley
Returns
0 upon success, other integers if otherwise
A Velocities structure, which defines the velocity of the trolley in the lateral plane
Precondition
This is called precisely once every BTI
Here is the call graph for this function:

◆ GetUserPosition()

int GetUserPosition ( Angles * angle,
Positions * pos,
Positions * result )

Obtains the User Position

Parameters
angleThe rope angle
posThe trolley position
resultA return parameter, which will become the position of the user
Returns
0 upon success, other integers if otherwise
A Positions structure, which defines the Position of the User in the lateral plane

◆ GetUserVelocity()

int GetUserVelocity ( Angles * angle,
Velocities * vel,
Velocities * result )

Obtains the User Velocity

Parameters
angleThe rope angle
velThe trolley velocity
resultA return parameter, which will become the velocity of the user
Returns
0 upon success, other integers if otherwise
A Velocities structure, which defines the Velocity of the User in the lateral plane
Here is the call graph for this function:

◆ HandleEncoderError()

static int HandleEncoderError ( Positions * curr_pos,
Velocities * curr_vel )
inlinestatic

Handles Error Processing from Position/Velocity Measurements

Parameters
curr_posThe current position
curr_velThe current velocity
Returns
0 upon no error, negative otherwise (using the universal error codes)
Postcondition
Iff negative is returned, both motors are switched off

Check Positional Limits first

Now, check velocity limits

Now, check if there is an encoder error

u_error = EENCR; conC_Encoder_initialize(myrio_session, &x_encoder, X_CONNECTOR_ID);

u_error = EENCR; conC_Encoder_initialize(myrio_session, &y_encoder, Y_CONNECTOR_ID);

Output Error

Here is the call graph for this function:

◆ HandlePotentiometerError()

static int HandlePotentiometerError ( Angles * curr_ang)
inlinestatic

Handles Error Processing for Potentiometer Measurements

Parameters
curr_angThe current angle reading
Returns
0 upon no error, ESTRN otherwise
Here is the call graph for this function:

◆ IOSetup()

int IOSetup ( )

Sets up the System-Sensor/Actuator Interface

Returns
0 upon success, negative otherwise

Setup Timer

Setup Encoders Channels

Setup Potentiometer Voltage Channels (are swapped)

Setup Motor Channels

Setup Keyboard Channels & Resources

Setup Reset flag

Calibration Message

Set Reference Positions

Setup the holding

Calibrate voltage intercepts for potentiometer

Here is the call graph for this function:

◆ IOShutdown()

int IOShutdown ( )

Shutsdown the System-Sensor/Actuator Interface

Returns
0 upon success, negative otherwise

Dissasociate with Encoders

Dissasociate with Potentiometers

Disassociate with Motor

Destroy Keyboard Lock

◆ KeyboardControlFork()

int KeyboardControlFork ( )

Enables Keyboard Control for Anti-Sway (concurrently)

Postcondition
If its already running, does nothing
Returns
0 upon success, negative if error

Begin Keyboard Thread

Here is the call graph for this function:

◆ KeyboardControlJoin()

int KeyboardControlJoin ( )

Stops Keyboard Control for Anti-Sway (concurrent process)

Returns
0 upon success, negative if error

Destroy Keymap Thread

◆ KeymapThread()

static void * KeymapThread ( void * resource)
inlinestatic

Obtains the numerical buttons pressed (1 through 9)

Returns
NULL
Postcondition
Updates keymap with all the number buttons, excluding 0, that are pressed

◆ PressedDelete()

bool PressedDelete ( )

Detects if the DEL key is pressed on the keyboard

Returns
true iff DEL is pressed on the keyboard

◆ Reset()

void Reset ( )

Resets GetTrolleyPosition and GetTrolleyVelocity by setting the velocity to zero

Postcondition
The next time GetTrolleyVelocity is called, both velocities are zero

◆ SetXVoltage()

int SetXVoltage ( Voltage voltage)

Sets the voltage of the X motor

Returns
0 upon success, other integers if otherwise

◆ SetYVoltage()

int SetYVoltage ( Voltage voltage)

Sets the voltage of the Y motor

Returns
0 upon success, other integers if otherwise

◆ wait()

static void wait ( )
inlinestatic

Waits for approximate 5 ms

Postcondition
About 5 ms have passed

Wait Constant

Variable Documentation

◆ enc_st_mask

const Encoder_StatusMask enc_st_mask
static
Initial value:
=
(Encoder_StError)

Encoder Error Mask.

◆ first_enc_state

int32_t first_enc_state[2]
static

First Encoder state for both the X and Y Encoders

◆ potentiometer_v_x_intercept

float potentiometer_v_x_intercept
static

Calibrated Voltage Intercept (x-intercept) for X Potentiometer

◆ potentiometer_v_y_intercept

float potentiometer_v_y_intercept
static

Calibrated Voltage Intercept (x-intercept) for Y Potentiometer

◆ prev_enc_state

int32_t prev_enc_state[2]
static

Previous Encoder state (from the last time either GetTrolleyPosition or GetTrolleyVelocity is caled), for both the X and Y Encoders

◆ timer

MyRio_IrqTimer timer

Universal Timer.

The Timer.