Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 8.10 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*Emma 5's Light
  2.  *Comprehensive integrated power management.
  3.  *This module will eventually take commands from Emma to change bus topology.
  4.  *Emma may also request the most recent averages for current and voltage.
  5.  
  6.  *Luke Dyson
  7.  *Spring 2010
  8.  */
  9.  
  10. //These definitions initialize the default values of the bus switches as not to interfere with the hardware defaults.
  11. //The stipulation here is that both batteries will be tied to the "quiet" bus (Bus A).
  12. //Since here we use solid state switches we will respond to boolean values appropriately.
  13. //Default solid state bus switch topology (Both batteries tied to quiet bus).
  14. #define swState1A 1
  15. #define swState2A 1
  16. #define swState1B 0
  17. #define swState2B 0
  18.  
  19. //Pin assignments for analog inputs.
  20. #define analogVPin1 1
  21. #define analogIPin1 2
  22. #define analogVPin2 3
  23. #define analogIPin2 4
  24.  
  25. //Analog read delay value (ms).
  26. #define analogDelay 3
  27.  
  28. //Default Pins for bus switch controls.
  29. //Do not use pin 13 because it will blink once on reset ALWAYS.
  30. #define sw1A 9
  31. #define sw1B 10
  32. #define sw2A 11
  33. #define sw2B 12
  34.  
  35. //Change this variable to affect the number of readings used in an average of current or voltage.
  36. #define bufferSize 10
  37.  
  38. //485 bus parameters.
  39. #define rx 2
  40. #define tx 3
  41. #define dataSize 4
  42. #define readTimeOut 500000 //define how long the readBus function has to run in microseconds
  43.  
  44. //485 bus global variables.
  45. byte dataIn[dataSize];
  46. byte dataOut[dataSize];
  47. byte tempByte;
  48. boolean dataFound;
  49. boolean dataToWrite;
  50. unsigned long readStart;
  51.  
  52. //Variables to store millisecond counts.
  53. long currentMillis = 0;
  54. long previousMillis = 0;
  55. long loopTime = 10;
  56.  
  57. //Variable to hold number of valid readings performed by the ADC.
  58. int Reads = 0;
  59.  
  60. //Arrays to hold voltage and current readings with least recently used update strategy.
  61. //Arrays to store electrical current measurements.
  62. int iBufferL[bufferSize];
  63. int iBufferQ[bufferSize];
  64.  
  65. //Arrays to store voltage measurements.
  66. int vBuffer1[bufferSize];
  67. int vBuffer2[bufferSize];
  68.  
  69. //Constants of proportionality for voltage different on hall sensor to true current (TBD).
  70. int c = 1;
  71. int d = 1;
  72.  
  73. //One array for each battery. First element in array is state of quiet bus switch (VN920) the second is the state of the noisy bus (MOSFET).
  74. //By default battery 1 is tied to the quiet bus.
  75. int batt1State[] = {0, 0};
  76. //By default battery 2 is tied to quiet bus.
  77. int batt2State[] = {0, 0};
  78.  
  79. //One array for each battery. Holds average voltage and current (in array respectively).
  80.  
  81. int batt1Avg[] = {0, 0};
  82. int batt2Avg[] = {0, 0};
  83.  
  84. //Flag to remember which battery was tied to the drive train.
  85.   int useFlag;
  86.  
  87. //Setup function runs once.
  88.  
  89. void setup() {
  90.  
  91.   //485 bus initialization code.
  92.   Serial.begin(19200);
  93.  
  94.   pinMode(rx, OUTPUT);
  95.   pinMode(tx, OUTPUT);
  96.   //both HIGH to transmit
  97.  
  98.   dataFound = false;
  99.   dataToWrite = false;
  100.  
  101.   digitalWrite(rx, LOW);
  102.   digitalWrite(tx, LOW);
  103.  
  104.   //Store present number of elapsed milliseconds.
  105.  
  106.   currentMillis = millis();
  107.  
  108.   //Initialize output pins for bus switches.
  109.  
  110.   pinMode(sw1A, OUTPUT);
  111.   pinMode(sw1B, OUTPUT);
  112.   pinMode(sw2A, OUTPUT);
  113.   pinMode(sw2B, OUTPUT);
  114.  
  115.   //Initialize Power Bus Configuration.
  116.  
  117.   digitalWrite(sw1A, HIGH);
  118.   digitalWrite(sw2A, HIGH);
  119.  
  120.   //Update battery bus indicator.
  121.   batt1State[0] = 1;
  122.   batt1State[0] = 1;
  123.  
  124.   //This will become a future problem with timer overflow to be addressed later.
  125.  
  126.   while(millis() < (currentMillis + loopTime)){
  127.  
  128.   }
  129.  
  130. }
  131.  
  132. void loop() {
  133.   //Store time at begining of loop.
  134.   currentMillis = millis();
  135.  
  136.   //Reset array position for next reading to reflect least recently used.
  137.   if(Reads == bufferSize){
  138.     Reads = 0;
  139.   }
  140.  
  141.   //Take readings of voltage off of both batteries.
  142.   //Factor of 4 is to compensate for the 4-to-1 voltage divider installed on all analog inputs for safety.
  143.   vBuffer1[Reads] = 4*analogRead(analogVPin1);
  144.   delay(analogDelay);
  145.   vBuffer2[Reads] = 4*analogRead(analogVPin2);
  146.   delay(analogDelay);
  147.   //Constants C and D proportionality between voltage deflection on hall sensor and current (TBD) on each bus.
  148.   iBufferL[Reads] = 4*c*analogRead(analogIPin1);
  149.   delay(analogDelay);
  150.   iBufferQ[Reads] = 4*d*analogRead(analogIPin2);
  151.  
  152.   Reads++;
  153.  
  154.   readBus(dataIn);
  155.   if(dataFound){
  156.     //someone said something to you
  157.     //carry on accordingly
  158.    
  159.     if(dataIn[1]==B10101000){
  160.       //Swap buses.
  161.       swapBus();
  162.     }else if(dataIn[1]==B10101001){
  163.       //We are being polled for data.
  164.       //Send two messages, one for each battery, last two fields are average voltage and current respectively.
  165.       //Second field is command field.
  166.       sendPowerData();
  167.     }else if(dataIn[1]==B10101010){
  168.       //Make power available to the noisy bus.
  169.       enablePower();
  170.     }
  171.   }
  172.  
  173. dataToWrite = true;
  174.   //if you have something to say, simply fill it into the 'dataOut' array and set dataToWrite=true
  175.   if(dataToWrite){
  176.  
  177.     writeBus(dataOut);
  178.   }
  179.  
  180.   //Sample code for printing to console
  181.   //Used to test Hall Sensors and Arduino ADC
  182.  
  183.   /*   Serial.println("Voltage battery 1:");
  184.    Serial.println(vStack1[Reads]);
  185.    Serial.println("Voltage battery 2:");
  186.    Serial.println(vStack2[Reads]);
  187.    Serial.println("Current battery 1:");
  188.    Serial.println(iStack1[Reads]);
  189.    Serial.println("Current battery 2:");
  190.    Serial.println(iStack2[Reads]);*/
  191.  
  192.   //This will become a future problem with timer overflow to be addressed later..//////
  193.  
  194.   while(millis() < (currentMillis + loopTime)){
  195.  
  196.   }
  197.  
  198. }
  199.  
  200. void swapBus(){
  201.   if(batt2State[1]==1){
  202.     //If battery two is on the loud bus swap it with 1.
  203.     batt2State[1]=0;
  204.     digitalWrite(sw2B, LOW);
  205.     //Switch battery two over to quiet bus.
  206.     batt2State[0]=1;
  207.     digitalWrite(sw2A, HIGH);
  208.     //Switch battery one off of quiet bus...
  209.     batt1State[0]=0;
  210.     digitalWrite(sw1A, LOW);
  211.     //...and onto the loud bus.
  212.     batt1State[1]=1;
  213.     digitalWrite(sw1B, HIGH);
  214.   }else{
  215.     //If battery one is on the loud bus swap it with 2.
  216.     batt1State[1]=0;
  217.     digitalWrite(sw1B, LOW);
  218.     //Switch battery one over to quiet bus.
  219.     batt1State[0]=1;
  220.     digitalWrite(sw1A, HIGH);
  221.     //Switch battery two off of quiet bus...
  222.     batt2State[0]=0;
  223.     digitalWrite(sw2A, LOW);
  224.     //...and onto the loud bus.
  225.     batt2State[1]=1;
  226.     digitalWrite(sw2B, HIGH);
  227.         //Else it's battery two and we need to swap it with battery 1.
  228.   }
  229. }
  230.  
  231. void sendPowerData(){
  232.  
  233. }
  234.  
  235. void enablePower(){
  236.   //Disconnect battery two from the quiet bus.
  237.   batt2State[0] = 0;
  238.   digitalWrite(sw2A, LOW);
  239.   //Connect battery two to the noisy bus.
  240.   batt2State[1] = 1;
  241.   digitalWrite(sw2B, HIGH);
  242. }
  243.  
  244. void writeBus(byte *dataOut){
  245.  
  246.   digitalWrite(rx, HIGH);
  247.   digitalWrite(tx, HIGH);
  248.   delayMicroseconds(1);  
  249.  
  250.   for(int i=0; i<dataSize; i++){
  251.     Serial.write(dataOut[i]);  
  252.   }
  253.  
  254.   dataToWrite = false;
  255.  
  256.   //stop writing
  257.   digitalWrite(rx, LOW);
  258.   digitalWrite(tx, LOW);
  259.   delayMicroseconds(1);
  260.  
  261. }
  262.  
  263.  
  264. void readBus(byte *dataIn){
  265.   readStart = micros();
  266.   dataFound = false;
  267.   while(Serial.available()>0 && (micros()-readStart)<readTimeOut){              //while there are bytes available in the serial buffer (128 max)...
  268.     tempByte = Serial.read();  //read them into 'tempByte'...
  269.     if(addressCheck(tempByte) && (micros()-readStart)<readTimeOut){           //if tempByte is an address you are looking for...
  270.  
  271.       while(Serial.available()<3 && (micros()-readStart)<readTimeOut){    //wait for the other bytes in the message to arrive or the whole group of 4 bytes will be lost
  272.         delayMicroseconds(1);                                              //they will probably be there anyway  
  273.       }
  274.  
  275.       dataFound = true;
  276.       dataIn[0] = tempByte;
  277.       for(int i=1; i<dataSize; i++){
  278.         dataIn[i] = Serial.read();      
  279.       }
  280.     }
  281.   }
  282.  
  283. }
  284.  
  285. boolean addressCheck(byte x){
  286.   //fill in this switch statement with whatever addresses you want to pay attention to  
  287.   switch (x){
  288.   case B00000011:         // normally an address you pay attention to would go here (Emma's power subprocessor address is 3)
  289.     return true;
  290.   default:
  291.     return false;
  292.   }
  293.  
  294. }