/**
 * Reads an accelerometer value from a single register. Reading from
 * the accelerometer is common for all directions.
 *
 * @param[in]  reg  The register address to read from
 * @param[in]  scale_weight The weight for the accelerometer values
 * @return the acceleration value in g's
 */
double Accel_ReadImpl(uint32_t reg, double scale_weight)
{
    NiFpga_Status status;
    uint16_t value;
    double scaledValue;

    /*
     * Get the value of the acceleration value register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_ReadU16(myrio_session, reg, &value);

    /*
     * Check if there was an error reading from the read register.
     *
     * If there was an error then the rest of the function cannot complete
     * correctly so print an error message to stdout and return from the
     * function early.
     */
    MyRio_ReturnValueIfNotSuccess(status, 0.0,
           "Could not read from the accelerometer value register!")

    /*
     * The value is always stored in an unsigned 16-bit register, but the value
     * is actually a signed 16-bit value. Cast this value directly to a signed
     * 16-bit value.
     */
    scaledValue = ((int16_t) value) * scale_weight;

    return scaledValue;
}
Beispiel #2
0
/**
 * Reads the encoder status, returning the status as bits.
 *
 * @param[in]  channel  A struct containing the registers on the encoder channel
 *                      to read.
 * @return              The status as a bit field.
 */
uint8_t Encoder_Status(MyRio_Encoder* channel)
{
    NiFpga_Status status;
    uint8_t statusValue;

    /*
     * Get the value of the status register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_ReadU8(myrio_session, channel->stat, &statusValue);

    /*
     * Check if there was an error reading from the encoder register.
     *
     * If there was an error then the status is undefined.rest Print an
     * error message to stdout and return a default status.
     */
    MyRio_ReturnValueIfNotSuccess(status, 0,
            "Could not read from the encoder status register!");

    /*
     *  Return the value of the status.
     */
    return statusValue;
}
Beispiel #3
0
/**
 * Reads the number of steps that the encoder has gone through. The behavior
 * depends on the SignalMode.
 *
 * ENC_QUAD_PHASE:
 * The counter increments when phase A leads phase B and decrements when phase
 * B leads phase A.
 *
 * ENC_SET_AND_DIRECTION:
 * The counter increments when the direction input is low and decrements when
 * the direction input is high.
 *
 * @param[in]  channel  A struct containing the registers on the encoder channel
 *                      to read.
 * @return              The status as a bit field.
 */
uint32_t Encoder_Counter(MyRio_Encoder* channel)
{
     NiFpga_Status status;
     uint32_t counterValue;

    /*
     * Get the value of the counter register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_ReadU32(myrio_session, channel->cntr, &counterValue);

    /*
     * Check if there was an error reading from the encoder register.
     *
     * If there was an error then the value of the counter is undefined
     * so print an error message to stdout and return 0.
     */
    MyRio_ReturnValueIfNotSuccess(status, 0,
            "Could not read from the encoder counter register!");

    /*
     * Return the value of the counter.
     */
    return counterValue;
}
Beispiel #4
0
/**
 * Reads a voltage value to from single channel. The channel structure must
 * previously been initialized with the appropriate scale factors with
 * AnalogScaling.
 *
 * @param[in]  channel  A struct containing the registers for the AIO
 *                      channel to be read from
 * @return the voltage value in volts
 */
double Aio_Read(MyRio_Aio* channel)
{
    NiFpga_Status status;
    uint16_t value = 0;
    double scaledValue;

    /*
     * Get the value of the value register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_ReadU16(myrio_session, channel->val, &value);

    /*
     * Check if there was an error reading from the read register.
     *
     * If there was an error then the rest of the function cannot complete
     * correctly so print an error message to stdout and return from the
     * function early. Return 0.0 as the read input value.
     */
    MyRio_ReturnValueIfNotSuccess(status, 0.0,
            "Could not read from the AI value registers!");

    /*
     * The value is always stored in an unsigned 16-bit register. For a signed
     * channel, cast this value directly to a signed 16-bit value.
     */
    if (channel->is_signed)
    {
        scaledValue = (int16_t)value * channel->scale_weight
                + channel->scale_offset;
    }
    else
    {
        scaledValue = value * channel->scale_weight + channel->scale_offset;
    }

    return scaledValue;
}
Beispiel #5
0
/**
 * Overview:
 * Demonstrates using the PWM. Generates a PWM signal from PWM 0 on
 * connector A.
 *
 * Instructions:
 * 1. Connect an oscilloscope to the PWM 0 pin on connector A.
 * 2. Run this program.
 *
 * Output:
 * The program generates a 25% duty cycle signal at 10 kHz for 60 s.
 *
 * Note:
 * The Eclipse project defines the preprocessor symbol for the NI myRIO-1900.
 * Change the preprocessor symbol to use this example with the NI myRIO-1950.
 */
int main(int argc, char **argv)
{
    NiFpga_Status status;

    MyRio_Pwm pwmA0;

    uint8_t selectReg;

    time_t currentTime;
    time_t finalTime;

    printf("PWM\n");

    /*
     * Initialize the PWM struct with registers from the FPGA personality.
     */
    pwmA0.cnfg = PWMA_0CNFG;
    pwmA0.cs = PWMA_0CS;
    pwmA0.max = PWMA_0MAX;
    pwmA0.cmp = PWMA_0CMP;
    pwmA0.cntr = PWMA_0CNTR;

    /*
     * Open the myRIO NiFpga Session.
     * This function MUST be called before all other functions. After this call
     * is complete the myRIO target will be ready to be used.
     */
    status = MyRio_Open();
    if (MyRio_IsNotSuccess(status))
    {
        return status;
    }

    /*
     * Set the waveform, enabling the PWM onboard device.
     */
    Pwm_Configure(&pwmA0, Pwm_Invert | Pwm_Mode,
            Pwm_NotInverted | Pwm_Enabled);

    /*
     * Set the clock divider. The internal PWM counter will increments at
     * f_clk / 4
     *
     * where:
     *  f_clk = the frequency of the myRIO FPGA clock (40 MHz default)
     */
    Pwm_ClockSelect(&pwmA0, Pwm_4x);

    /*
     * Set the maximum counter value. The counter counts from 0 to 1000.
     *
     * The counter increments at 40 MHz / 4 = 10 MHz and the counter counts
     * from 0 to 1000. The frequency of the PWM waveform is 10 MHz / 1000
     * = 10 kHz.
     */
    Pwm_CounterMaximum(&pwmA0, 1000);

    /*
     * Set the comparison counter value. The PWM counter counts from 0 to 1000
     * and outputs from 0 to the comparison value (250).
     *
     * The duty cycle is 250 / 1000 = 25%.
     */
    Pwm_CounterCompare(&pwmA0, 250);

    /*
     * PWM outputs are on pins shared with other onboard devices. To output on
     * a physical pin, select the PWM on the appropriate SELECT register. See
     * the MUX example for simplified code to enable-disable onboard devices.
     *
     * Read the value of the SYSSELECTA register.
     */
    status = NiFpga_ReadU8(myrio_session, SYSSELECTA, &selectReg);
    MyRio_ReturnValueIfNotSuccess(status, status,
        "Could not read from the SYSSELECTA register!")

    /*
     * Set bit2 of the SYSSELECTA register to enable PWMA_0 functionality.
     * The functionality of the bit is specified in the documentation.
     */

    selectReg = selectReg | (1 << 2);

    /*
     * Write the updated value of the SYSSELECTA register.
     */
    status = NiFpga_WriteU8(myrio_session, SYSSELECTA, selectReg);
    MyRio_ReturnValueIfNotSuccess(status, status,
        "Could not write to the SYSSELECTA register!")

    /*
     * Normally, the main function runs a long running or infinite loop.
     * Keep the program running for 60 seconds so that the PWM output can be
     * observed using an external instrument.
     */
    time(&currentTime);
    finalTime = currentTime + LoopDuration;
    while (currentTime < finalTime)
    {
        time(&currentTime);
    }

    /*
     * Close the myRIO NiFpga Session.
     * This function MUST be called after all other functions.
     */
    status = MyRio_Close();

    /*
     * Returns 0 if successful.
     */
    return status;
}