Advertisement
RuiViana

Power_C_Frequencia

Oct 15th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. ///////////////////////////////////////////
  2. // TCC - USF Campinas/SP - Eng. Elétrica //
  3. // Qualidade de Energia //
  4. //---------------------------------------//
  5. // Denis Eduardo Donadon Torres //
  6. ///////////////////////////////////////////
  7.  
  8. #include <Arduino.h>
  9. #include "EmonLibPro.h"
  10. #include <DS1307.h> // Carrega a biblioteca do RTC DS1307 (Real Time Clock)
  11. #include <SD.h> // Carrega a biblioteca SD
  12.  
  13. int userCommand;
  14. DS1307 rtc(A4, A5); // Modulo RTC DS1307 ligado as portas A4 e A5 do Arduino
  15. const int chipSelect = 8; // Pino do Arduino conectado ao pino CS do modulo
  16. float Q; // Potência Reativa
  17.  
  18. EmonLibPro Emon;
  19. Sd2Card SDcard;
  20. SdVolume volume;
  21.  
  22. void setup()
  23. {
  24. rtc.halt(false); // Aciona o relogio
  25. /////////////////////////////////////////////////////////////////////////////////////////////////////
  26. // Usar estas linhas somente para ajustar o relógio no RTC na primeira vez que for "setar" o mesmo //
  27. //-------------------------------------------------------------------------------------------------//
  28. // rtc.setDOW(FRIDAY); //Define o dia da semana //
  29. // rtc.setTime(13, 51, 0); //Define o horario //
  30. // rtc.setDate(02, 10, 2015); //Define o dia, mês e ano //
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////
  32. rtc.setSQWRate(SQW_RATE_1); // Definicoes do pino SQW/Out
  33. rtc.enableSQW(true);
  34.  
  35. Serial.begin(9600);
  36.  
  37. /////////////////////////////////////////////////////////////
  38. // Testa se o cartão cartão SD está encaixado corretamente //
  39. /////////////////////////////////////////////////////////////
  40. if (!SD.begin(chipSelect))
  41. {
  42. Serial.println();
  43. Serial.println("Falha ao acessar o cartao SD!");
  44. Serial.println("Verifique o cartao e reinicie o Analisador...");
  45. Serial.println();
  46. return;
  47. }
  48. Serial.println();
  49. Serial.println("Cartao iniciado corretamente!");
  50. Serial.println();
  51.  
  52. Emon = EmonLibPro(); // Required
  53. Emon.begin(); // Required
  54.  
  55. printMenu();
  56. }
  57.  
  58. void loop()
  59. {
  60. if(userCommand == 1 && Emon.FlagCALC_READY)
  61. {
  62. Emon.calculateResult();
  63. for (byte i=0;i<CURRENTCOUNT;i++)
  64. {
  65. printResults(i);
  66. }
  67. }
  68. if(userCommand == 2 && Emon.FlagCYCLE_FULL) {
  69. for (byte i=0;i<VOLTSCOUNT + CURRENTCOUNT;i++){
  70. Serial.print("ADC");
  71. Serial.print(0+i);
  72. Serial.print(",");
  73. serialDataTable(i);
  74. }
  75. Emon.FlagCYCLE_FULL = false; //Must reset after read to know next batch.
  76. }
  77.  
  78. if(userCommand == 3 && Emon.FlagCALC_READY) {
  79. Emon.printStatus();
  80. userCommand = 0;
  81. }
  82.  
  83. if (userCommand == 4 && EmonLibPro::FlagOutOfTime) { // This should never be true. Reduce sampling rate if it is!
  84. Serial.println("$"); // Alarm that previous ADC processing has run out of time before timer event. Must decrease SAMPLESPSEC
  85. }
  86.  
  87. if(Serial.available())
  88. {
  89. userCommand=Serial.parseInt();
  90. Serial.print("Voce digitou: ");
  91. Serial.println(userCommand);
  92. Serial.println();
  93. Serial.println("Ok, seguem abaixo:");
  94. Serial.println();
  95. Serial.println("Dados: ");
  96. Serial.println();
  97. }
  98. }
  99.  
  100. void printMenu()
  101. {
  102. Serial.println("ANALISADOR DE QUALIDADE DE ENERGIA");
  103. Serial.println();
  104. Serial.println("Digite o n 1 e tecle ENTER para iniciar!");
  105. Serial.println("Aguardando seu comando...");
  106. }
  107.  
  108. // Print calculated results
  109. void printResults(byte i)
  110. {
  111. Q = sqrt(Emon.ResultP[i].P * Emon.ResultP[i].P + Emon.ResultP[i].S * Emon.ResultP[i].S); // Calcula a Potêcia Reativa ( Q = RAIZ(Potência Ativa² + Potência Aparente² ).
  112.  
  113. // Serial.print(rtc.getDOWStr(FORMAT_LONG));
  114. Serial.print(", ");
  115. // Serial.print(rtc.getDateStr(FORMAT_SHORT));
  116. Serial.print(", ");
  117. Serial.print("as ");
  118. // Serial.print(rtc.getTimeStr());
  119. Serial.print(" hrs");
  120. Serial.println();
  121.  
  122. Serial.print("FP: ");
  123. Serial.print(Emon.ResultP[i].F);
  124. Serial.print("\t");
  125.  
  126. Serial.print("Freq: ");
  127. Serial.print(Emon.ResultV[0].HZ);
  128. Serial.print(" Hz\t");
  129.  
  130. Serial.print("Volts: ");
  131. Serial.print(Emon.ResultV[0].U);
  132. Serial.print(" Vac\t");
  133.  
  134. Serial.print("Corrente: ");
  135. Serial.print(Emon.ResultP[i].I);
  136. Serial.print(" A\t");
  137.  
  138. Serial.print("Pot Ativa: ");
  139. Serial.print(Emon.ResultP[i].P);
  140. Serial.print(" W\t");
  141.  
  142. Serial.print("Pot Aparente: ");
  143. Serial.print(Emon.ResultP[i].S);
  144. Serial.print(" VA\t");
  145.  
  146. Serial.print("Pot Reativa: ");
  147. Serial.print(Q);
  148. Serial.println(" var");
  149.  
  150. //////////////////////////////////////////////
  151. // Cria o arquivo arquivo .xls no cartão SD //
  152. //////////////////////////////////////////////
  153.  
  154. File dataFile = SD.open("Analise.xls", FILE_WRITE); // * Atenção, o nome do arquivo não pode ter caracteres inválidos como ()/{}[].,;* nem com acentos!!! *
  155. if (dataFile) // Grava os dados no arquivo
  156. {
  157.  
  158. // dataFile.print(rtc.getDOWStr(FORMAT_LONG));
  159. dataFile.print(", ");
  160. // dataFile.print(rtc.getDateStr(FORMAT_SHORT));
  161. dataFile.print(", ");
  162. dataFile.print("as ");
  163. // dataFile.print(rtc.getTimeStr());
  164. dataFile.print(" hrs");
  165. dataFile.println();
  166.  
  167. dataFile.print("FP: ");
  168. dataFile.print(Emon.ResultP[i].F);
  169. dataFile.print("\t");
  170.  
  171. dataFile.print("Freq: ");
  172. dataFile.print(Emon.ResultV[0].HZ);
  173. dataFile.print(" Hz\t");
  174.  
  175. dataFile.print("Volts: ");
  176. dataFile.print(Emon.ResultV[0].U);
  177. dataFile.print(" V\t");
  178.  
  179. dataFile.print("Corrente: ");
  180. dataFile.print(Emon.ResultP[i].I);
  181. dataFile.print(" A\t");
  182.  
  183. dataFile.print("Pot Ativa: ");
  184. dataFile.print(Emon.ResultP[i].P);
  185. dataFile.print(" W\t");
  186.  
  187. dataFile.print("Pot Aparente: ");
  188. dataFile.print(Emon.ResultP[i].S);
  189. dataFile.print(" VA\t");
  190.  
  191. dataFile.print("Potencia Reativa: ");
  192. dataFile.print(Q);
  193. dataFile.print(" var");
  194. dataFile.println(); // pula a linha
  195. dataFile.println(); // pula a linha
  196.  
  197. dataFile.close();
  198. }
  199. else
  200. {
  201. // Mensagem de erro caso ocorra algum problema na criação do arquivo
  202. Serial.println("Erro ao criar o arquivo .txt!");
  203. Serial.println("Verifique o cartao e reinicie o Analisador...");
  204. Serial.println();
  205. }
  206.  
  207. #ifdef USEPLL
  208. if(!Emon.pllUnlocked) Serial.print(" Locked");
  209. #endif
  210. Serial.println("\t");
  211. }
  212.  
  213. // Exposes the internal sampled values for all samples on a cycle
  214. // Usefull to display the signal on a graph.
  215. void serialDataTable(byte b)
  216. {
  217. int sizearr = (Emon.SamplesPerCycleTotal/Emon.CyclesPerTotal);
  218. if (sizearr > CYCLEARRSIZE) sizearr = CYCLEARRSIZE;
  219. for (byte i=0;i<=sizearr;i++)
  220. {
  221. Serial.print(Emon.Sample[b].CycleArr[i]);
  222. Serial.print(",");
  223. }
  224. Serial.println("\t");
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement