Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. #include <StaticThreadController.h>
  2. #include <Thread.h>
  3. #include <ThreadController.h>
  4.  
  5. #include <Wire.h>
  6. #include <Adafruit_ADS1015.h>
  7. #include <HX711_ADC.h>
  8.  
  9. //ADS1015 constructor
  10. Adafruit_ADS1015 ads(0x49);
  11.  
  12. //HX711 constructor (DT pin, SCK pin)
  13. HX711_ADC LoadCell_Horizontal(3, 4);
  14. HX711_ADC LoadCell_FrontalDireita(5, 6);
  15. HX711_ADC LoadCell_FrontalEsquerda(7, 8);
  16. HX711_ADC LoadCell_TraseiraDireita(9, 10);
  17. HX711_ADC LoadCell_TraseiraEsquerda(11, 12);
  18.  
  19. class PitotThread: public Thread
  20. {
  21. public:
  22. float Voltage = 0.0;
  23. int16_t adc = 0;
  24. int16_t adc_port;
  25.  
  26. void run(){
  27. adc = ads.readADC_SingleEnded(adc_port);
  28. Voltage = (adc * 0.1875)/1000;
  29. runned();
  30. }
  31. };
  32.  
  33. class CellsThread: public Thread
  34. {
  35. public:
  36. float forca_horizontal;
  37. float forca_frontal_direita;
  38. float forca_frontal_esquerda;
  39. float forca_traseira_direita;
  40. float forca_traseira_esquerda;
  41. char inByte;
  42.  
  43. void checkTare(){
  44. if (inByte == 't') {
  45. LoadCell_FrontalDireita.tareNoDelay();
  46. LoadCell_FrontalEsquerda.tareNoDelay();
  47. LoadCell_TraseiraDireita.tareNoDelay();
  48. LoadCell_TraseiraEsquerda.tareNoDelay();
  49. }
  50.  
  51. //check if last tare operation is complete
  52.  
  53. if (LoadCell_FrontalDireita.getTareStatus() == true) {
  54. Serial.println("Tare load cell 1 complete");
  55. }
  56. if (LoadCell_FrontalEsquerda.getTareStatus() == true) {
  57. Serial.println("Tare load cell 2 complete");
  58. }
  59. if (LoadCell_TraseiraDireita.getTareStatus() == true) {
  60. Serial.println("Tare load cell 3 complete");
  61. }
  62. if (LoadCell_TraseiraEsquerda.getTareStatus() == true) {
  63. Serial.println("Tare load cell 4 complete");
  64. }
  65. }
  66.  
  67. void initialize(){
  68. LoadCell_FrontalDireita.begin();
  69. LoadCell_FrontalEsquerda.begin();
  70. LoadCell_TraseiraDireita.begin();
  71. LoadCell_TraseiraEsquerda.begin();
  72.  
  73. long calibrationTime = 5000; // tare preciscion can be improved by adding a few seconds of stabilising time
  74.  
  75. byte LoadCell_FrontalDireita_ready = 0;
  76. byte LoadCell_FrontalEsquerda_ready = 0;
  77. byte LoadCell_TraseiraDireita_ready = 0;
  78. byte LoadCell_TraseiraEsquerda_ready = 0;
  79.  
  80. while ((LoadCell_FrontalDireita_ready + LoadCell_FrontalEsquerda_ready + LoadCell_TraseiraDireita_ready + LoadCell_TraseiraEsquerda_ready) < 4) { //run startup, stabilization and tare, both modules simultaniously
  81. if (!LoadCell_FrontalDireita_ready) LoadCell_FrontalDireita_ready = LoadCell_FrontalDireita.startMultiple(calibrationTime);
  82. if (!LoadCell_FrontalEsquerda_ready) LoadCell_FrontalEsquerda_ready = LoadCell_FrontalEsquerda.startMultiple(calibrationTime);
  83. if (!LoadCell_TraseiraDireita_ready) LoadCell_TraseiraDireita_ready = LoadCell_TraseiraDireita.startMultiple(calibrationTime);
  84. if (!LoadCell_TraseiraEsquerda_ready) LoadCell_TraseiraEsquerda_ready = LoadCell_TraseiraEsquerda.startMultiple(calibrationTime);
  85. }
  86.  
  87. LoadCell_FrontalDireita.setCalFactor(744.0); // user set calibration factor (float)
  88. LoadCell_FrontalEsquerda.setCalFactor(744.0); // user set calibration factor (float)
  89. LoadCell_TraseiraDireita.setCalFactor(744.0); // user set calibration factor (float)
  90. LoadCell_TraseiraEsquerda.setCalFactor(744.0); // user set calibration factor (float)
  91. Serial.println("Startup + tare is complete");
  92. }
  93.  
  94. void run(){
  95. LoadCell_FrontalDireita.update();
  96. LoadCell_FrontalEsquerda.update();
  97. LoadCell_TraseiraDireita.update();
  98. LoadCell_TraseiraEsquerda.update();
  99.  
  100. forca_frontal_direita = LoadCell_FrontalDireita.getData();
  101. forca_frontal_esquerda = LoadCell_FrontalEsquerda.getData();
  102. forca_traseira_direita = LoadCell_TraseiraDireita.getData();
  103. forca_traseira_esquerda = LoadCell_TraseiraEsquerda.getData();
  104.  
  105. if (Serial.available() > 0) {
  106. inByte = Serial.read();
  107. }
  108.  
  109. runned();
  110. }
  111. };
  112.  
  113. ThreadController controller = ThreadController();
  114. PitotThread pitot0 = PitotThread();
  115. PitotThread pitot1 = PitotThread();
  116. PitotThread pitot2 = PitotThread();
  117. PitotThread pitot3 = PitotThread();
  118. CellsThread celulas_bancada = CellsThread();
  119.  
  120. void setup(){
  121. Serial.begin(9600);
  122.  
  123. pitot0.adc_port = 0;
  124. pitot1.adc_port = 1;
  125. pitot2.adc_port = 2;
  126. pitot3.adc_port = 3;
  127.  
  128. pitot0.setInterval(10);
  129. pitot1.setInterval(10);
  130. pitot2.setInterval(10);
  131. pitot3.setInterval(10);
  132.  
  133. celulas_bancada.initialize();
  134. celulas_bancada.setInterval(10);
  135.  
  136. controller.add(&pitot0);
  137. controller.add(&pitot1);
  138. controller.add(&pitot2);
  139. controller.add(&pitot3);
  140. controller.add(&celulas_bancada);
  141.  
  142. ads.begin();
  143. }
  144.  
  145. void loop(){
  146.  
  147. controller.run();
  148.  
  149. // Serial.print(50*pitot0.Voltage);
  150. // Serial.print("\t");
  151. //
  152. // Serial.print(50*pitot1.Voltage);
  153. // Serial.print("\t");
  154. //
  155. // Serial.print(50*pitot2.Voltage);
  156. // Serial.print("\t");
  157. //
  158. // Serial.print(50*pitot3.Voltage);
  159. // Serial.print("\t");
  160. //
  161. // Serial.print(celulas_bancada.forca_frontal_direita);
  162. // Serial.print("\t");
  163. //
  164. // Serial.print(celulas_bancada.forca_frontal_esquerda);
  165. // Serial.print("\t");
  166. //
  167. // Serial.print(celulas_bancada.forca_traseira_direita);
  168. // Serial.print("\t");
  169. //
  170. // Serial.print(celulas_bancada.forca_traseira_esquerda);
  171. // Serial.print("\t");
  172. //
  173. // Serial.println();
  174.  
  175. Serial.print("!");
  176.  
  177. // Serial.print("fh");
  178. // Serial.print("=");
  179. // Serial.print(cells.fh);
  180. // Serial.print(";");
  181.  
  182. Serial.print("ffd");
  183. Serial.print("=");
  184. Serial.print(celulas_bancada.forca_frontal_direita);
  185. Serial.print(";");
  186.  
  187. Serial.print("ffe");
  188. Serial.print("=");
  189. Serial.print(celulas_bancada.forca_frontal_esquerda);
  190. Serial.print(";");
  191.  
  192. Serial.print("ftd");
  193. Serial.print("=");
  194. Serial.print(celulas_bancada.forca_traseira_direita);
  195. Serial.print(";");
  196.  
  197. Serial.print("fte");
  198. Serial.print("=");
  199. Serial.print(celulas_bancada.forca_traseira_esquerda);
  200. Serial.print(";");
  201.  
  202. Serial.print("pitot0");
  203. Serial.print("=");
  204. Serial.print(pitot0.Voltage);
  205. Serial.print(";");
  206.  
  207. Serial.print("pitot1");
  208. Serial.print("=");
  209. Serial.print(pitot1.Voltage);
  210. Serial.print(";");
  211.  
  212. Serial.print("pitot2");
  213. Serial.print("=");
  214. Serial.print(pitot2.Voltage);
  215. Serial.print(";");
  216.  
  217. Serial.print("pitot3");
  218. Serial.print("=");
  219. Serial.print(pitot3.Voltage);
  220. Serial.print(";");
  221.  
  222. Serial.println("@");
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement