ComputerModel.java 7.4 KB

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