Guest User

Untitled

a guest
Nov 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2015,
  3. * National Instruments Corporation.
  4. * All rights reserved.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <time.h>
  9. #include "MyRio.h"
  10.  
  11.  
  12. // global session variable, defined in myRIO.c
  13. extern NiFpga_Session myrio_session;
  14.  
  15. /**
  16. * Write a voltage value to a single AIO pin.
  17. *
  18. * @param[in] valReg the pin's value register to write to
  19. * @param[in] goReg the pin's go register
  20. * @param[in] weight the pin's weight value
  21. * @param[in] offset the pin's offset value
  22. * @param[in] isSigned whether or not the pin takes a signed voltage
  23. * @param[in] value the voltage value in volts
  24. */
  25. void analogWrite(uint32_t valReg, uint32_t goReg, uint32_t weight,
  26. uint32_t offset, NiFpga_Bool isSigned, double value) {
  27.  
  28. // compute scaled weight and offset
  29. double scaledWeight = weight / 1000000000.0;
  30. double scaledOffset = offset / 1000000000.0;
  31.  
  32. uint16_t outputValue;
  33.  
  34. // convert voltage to appropriate register value using weight and offset
  35. if (isSigned) {
  36. value = (value - scaledOffset) / scaledWeight;
  37. value = (value < INT16_MIN) ? INT16_MIN : value;
  38. value = (value > INT16_MAX) ? INT16_MAX : value;
  39.  
  40. // round the scaled value to the nearest integer
  41. value += (value < 0.0) ? -0.5 : 0.5;
  42.  
  43. outputValue = (uint16_t)((int16_t)(value));
  44. } else {
  45. value = (value - scaledOffset) / scaledWeight + 0.5;
  46. value = (value < 0) ? 0 : value;
  47. value = (value > UINT16_MAX) ? UINT16_MAX : value;
  48. outputValue = (uint16_t) value;
  49. }
  50.  
  51. NiFpga_Status status;
  52.  
  53. // write the value to the value register
  54. status = NiFpga_WriteU16(myrio_session, valReg, outputValue);
  55. MyRio_ReturnIfNotSuccess(status, "Error writing to valReg");
  56.  
  57. // write to the GO register to actually output the value
  58. status = NiFpga_WriteU16(myrio_session, goReg, 1);
  59. MyRio_ReturnIfNotSuccess(status, "Error writing to goReg");
  60. }
  61.  
  62. void delay(time_t seconds) {
  63. time_t currentTime;
  64. time_t finalTime;
  65. time(&currentTime);
  66. finalTime = currentTime + seconds;
  67. while (currentTime < finalTime) time(&currentTime);
  68. }
  69.  
  70.  
  71. // blinks LED0 off and on
  72. int main(int argc, char **argv)
  73. {
  74. // don't buffer stdout (required for logging to work)
  75. setbuf(stdout, NULL);
  76.  
  77.  
  78. printf("Air bearing test\n");
  79.  
  80. NiFpga_Status status;
  81.  
  82. /*
  83. * Open the myRIO NiFpga Session.
  84. * This function MUST be called before all other functions. After this call
  85. * is complete the myRIO target will be ready to be used.
  86. */
  87. status = MyRio_Open();
  88. if (MyRio_IsNotSuccess(status))
  89. {
  90. printf("Failed to initialize NiFpga session\n");
  91. return status;
  92. }
  93.  
  94. while (1) {
  95. printf("Turning on top right\n");
  96. analogWrite(AOA_0VAL, AOSYSGO, AOA_0WGHT, AOA_0OFST, NiFpga_False, 0.3);
  97.  
  98. printf("Turning on top left\n");
  99. analogWrite(AOA_1VAL, AOSYSGO, AOA_1WGHT, AOA_1OFST, NiFpga_False, 0.3);
  100.  
  101. printf("Turning on bottom left\n");
  102. analogWrite(AOB_0VAL, AOSYSGO, AOB_0WGHT, AOB_0OFST, NiFpga_False, 0.3);
  103.  
  104. printf("Turning on bottom right\n");
  105. analogWrite(AOB_1VAL, AOSYSGO, AOB_1WGHT, AOB_1OFST, NiFpga_False, 0.3);
  106.  
  107. printf("Waiting...\n");
  108.  
  109. delay(10);
  110.  
  111. printf("Turning off top right\n");
  112. analogWrite(AOA_0VAL, AOSYSGO, AOA_0WGHT, AOA_0OFST, NiFpga_False, 0);
  113.  
  114. printf("Turning off top left\n");
  115. analogWrite(AOA_1VAL, AOSYSGO, AOA_1WGHT, AOA_1OFST, NiFpga_False, 0);
  116.  
  117. printf("Turning off bottom left\n");
  118. analogWrite(AOB_0VAL, AOSYSGO, AOB_0WGHT, AOB_0OFST, NiFpga_False, 0);
  119.  
  120. printf("Turning off bottom right\n");
  121. analogWrite(AOB_1VAL, AOSYSGO, AOB_1WGHT, AOB_1OFST, NiFpga_False, 0);
  122.  
  123. delay(10);
  124. }
  125. }
Add Comment
Please, Sign In to add comment