ComputerModel.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package camsucks;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.InputStreamReader;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. * This is the model of this project
  15. *
  16. * This model contains two main data members as well as some data members used
  17. * to configure the calculations and two checks
  18. *
  19. * Along with all the getters and setters this model has two methods used for
  20. * polling and calculating
  21. *
  22. * @author Roel
  23. */
  24. public class ComputerModel {
  25. // The main components of the Computermodel
  26. private Sensor sensor;
  27. private GRID grid;
  28. // The variables used to calculate the percentage to send
  29. private double maxCPUTemp;
  30. private double maxGPUTemp;
  31. private double targetCPUTemp;
  32. private double targetGPUTemp;
  33. private double targetRPM;
  34. // a global minimum percentage, this is used to prevent the controller to constantly turn the fans on and off
  35. private int minRPM;
  36. //Boolean Checks used to know whether to run certain pieces of code
  37. private boolean extraPoll;
  38. private boolean manual;
  39. // private boolean GRIDset;
  40. // The percentage to send at the end of the loop.
  41. private int percentageToSend;
  42. private int ticks;
  43. /**
  44. *
  45. *
  46. * All members get initialised here.
  47. *
  48. * @param selectedPort
  49. */
  50. public ComputerModel(String selectedPort) {
  51. targetRPM = 35;
  52. targetCPUTemp = 50;
  53. maxCPUTemp = 70;
  54. targetGPUTemp = 50;
  55. maxGPUTemp = 70;
  56. minRPM = 35;
  57. extraPoll = true;
  58. manual = false;
  59. ticks = 0;
  60. grid = new GRID(selectedPort);
  61. try {
  62. sensor = new Sensor();
  63. } catch (Exception ex) {
  64. Logger.getLogger(ComputerModel.class.getName()).log(Level.SEVERE, null, ex);
  65. }
  66. }
  67. /**
  68. *
  69. * Currently only the pollCPUPackageTemp method of the sensor object is
  70. * called This can change in the future
  71. *
  72. */
  73. public void poll() {
  74. //System.out.println("Polling...");
  75. if (isExtraPoll()) {
  76. ticks++;
  77. if (!getGrid().isSame() || ticks > 10) {
  78. //System.out.println("polling some extra shit ");
  79. ticks = 0;
  80. getGrid().pollVoltage();
  81. for (int i = 1; i < 7; i++) {
  82. getGrid().pollFanAMP(i);
  83. getGrid().pollFanRPM(i);
  84. }
  85. }
  86. }
  87. try {
  88. getSensor().pollCPUTemp();
  89. getSensor().pollCPUMax();
  90. getSensor().pollGPUTemp();
  91. getSensor().pollGPUMax();
  92. } catch (Exception ex) {
  93. System.out.println("Polling Failed");
  94. }
  95. }
  96. /**
  97. *
  98. * In this method the percentage to sent is computed. The calculation aims
  99. * to act as a proportional controller. A later step could be to add an
  100. * integral controller to the calculation to get a better calculated fan
  101. * curve
  102. *
  103. */
  104. public void compute() {
  105. double currentCPUTemp = getSensor().getCPUTemp();
  106. double CPUerror = (currentCPUTemp - getTargetCPUTemp());
  107. double CPUkFactor = ((100 - getTargetRPM()) / (getMaxCPUTemp() - getTargetCPUTemp()));
  108. int tempCPUPercentage = (int) (getTargetRPM() + (CPUkFactor * CPUerror));
  109. if (tempCPUPercentage < minRPM) {
  110. tempCPUPercentage = minRPM;
  111. }
  112. double currentGPUTemp = getSensor().getGPUTemp();
  113. double GPUerror = (currentGPUTemp - getTargetGPUTemp());
  114. double GPUkFactor = ((100 - getTargetRPM()) / (getMaxGPUTemp() - getTargetGPUTemp()));
  115. int tempGPUPercentage = (int) (getTargetRPM() + (GPUkFactor * GPUerror));
  116. if (tempGPUPercentage < minRPM) {
  117. tempGPUPercentage = minRPM;
  118. }
  119. if(tempCPUPercentage > tempGPUPercentage){
  120. setPercentageToSend(tempCPUPercentage);
  121. } else {
  122. setPercentageToSend(tempGPUPercentage);
  123. }
  124. }
  125. /**
  126. * A getter for the Sensor Object to make the methods of the object
  127. * available
  128. *
  129. * @return the sensor
  130. */
  131. public Sensor getSensor() {
  132. return sensor;
  133. }
  134. /**
  135. * A getter for the GRID Object to make the methods of the object available
  136. *
  137. * @return the grid
  138. */
  139. public GRID getGrid() {
  140. return grid;
  141. }
  142. /**
  143. * This setter overwrites the old GRID with a new Object with the
  144. * selectedport as the COM port to connect to
  145. *
  146. * @param selectedPort The com port the GRID controller is located at
  147. */
  148. public void setGrid(String selectedPort) {
  149. grid = new GRID(selectedPort);
  150. }
  151. /**
  152. * Used to check if the extra polling has to be exicuted
  153. *
  154. * @return the extraPoll
  155. */
  156. public boolean isExtraPoll() {
  157. return extraPoll;
  158. }
  159. /**
  160. * Used to define whether the extra polling must be done
  161. *
  162. * @param extraPoll the extraPoll to set
  163. */
  164. public void setExtraPoll(boolean extraPoll) {
  165. this.extraPoll = extraPoll;
  166. }
  167. /**
  168. *
  169. * @return the targetRPM
  170. */
  171. public double getTargetRPM() {
  172. return targetRPM;
  173. }
  174. /**
  175. * @param targetRPM the targetRPM to set
  176. */
  177. public void setTargetRPM(double targetRPM) {
  178. this.targetRPM = targetRPM;
  179. }
  180. /**
  181. * @return the targetTemp
  182. */
  183. public double getTargetCPUTemp() {
  184. return targetCPUTemp;
  185. }
  186. /**
  187. * @param targetTemp the targetTemp to set
  188. */
  189. public void setTargetCPUTemp(double targetTemp) {
  190. this.targetCPUTemp = targetTemp;
  191. }
  192. /**
  193. * @return the maxTemp
  194. */
  195. public double getMaxCPUTemp() {
  196. return maxCPUTemp;
  197. }
  198. /**
  199. * @param maxTemp the maxTemp to set
  200. */
  201. public void setMaxCPUTemp(double maxTemp) {
  202. this.maxCPUTemp = maxTemp;
  203. }
  204. /**
  205. * @return the targetTemp
  206. */
  207. public double getTargetGPUTemp() {
  208. return targetGPUTemp;
  209. }
  210. /**
  211. * @param targetTemp the targetTemp to set
  212. */
  213. public void setTargetGPUTemp(double targetTemp) {
  214. this.targetGPUTemp = targetTemp;
  215. }
  216. /**
  217. * @return the maxTemp
  218. */
  219. public double getMaxGPUTemp() {
  220. return maxGPUTemp;
  221. }
  222. /**
  223. * @param maxTemp the maxTemp to set
  224. */
  225. public void setMaxGPUTemp(double maxTemp) {
  226. this.maxGPUTemp = maxTemp;
  227. }
  228. /**
  229. * A check to see whether the percentage to send is manually set or has to
  230. * be calculated
  231. *
  232. * @return the manual
  233. */
  234. public boolean isManual() {
  235. return manual;
  236. }
  237. /**
  238. * @param manual the manual to set
  239. */
  240. public void setManual(boolean manual) {
  241. this.manual = manual;
  242. }
  243. /**
  244. * @return the minRPM
  245. */
  246. public double getMinRPM() {
  247. return minRPM;
  248. }
  249. /**
  250. *
  251. * @param minRPM
  252. */
  253. public void setMinRPM(int minRPM) {
  254. this.minRPM = minRPM;
  255. }
  256. /**
  257. * @return the percentageToSend
  258. */
  259. public int getPercentageToSend() {
  260. return percentageToSend;
  261. }
  262. /**
  263. * @param percentageToSend the percentageToSend to set
  264. */
  265. public void setPercentageToSend(int percentageToSend) {
  266. this.percentageToSend = percentageToSend;
  267. }
  268. }