tweaked again, now heading to CERN for testing

This commit is contained in:
James Devine 2017-03-26 15:32:47 +02:00
parent 4efa743882
commit cbd1c6fdab
3 changed files with 111 additions and 72 deletions

View file

@ -25,10 +25,12 @@ int calcthreshCh2 = 0;
int Ch1SetPoint = 0; int Ch1SetPoint = 0;
int Ch2SetPoint = 0; int Ch2SetPoint = 0;
int VoltageSetPoint = 0; int VoltageSetPoint = 0;
int VoltageOffset = 6; //arbitrary correction factor for the voltage, higher numbers = higher voltage
int CMFEvents = 0; int CMFEvents = 0;
float EventRate = 0; float EventRate = 0;
float PressureTempVal = 0; float PressureTempVal = 0;
float PressureTempValOld = 0; float PressureTempValOld = 0;
bool Checktemp = false;
//SoftSPI pin assignments //SoftSPI pin assignments
#define SS_pin 42 #define SS_pin 42
@ -72,32 +74,15 @@ const int HVSetpoints[50] = {98, 98, 97, 97, 96, 96, 95, 95, 94, 94,
//HV setpoints, starting from 0 to 50 degrees (i.e. 0th element is 0 degrees) - add 0.5 and cast as an int for the index. //HV setpoints, starting from 0 to 50 degrees (i.e. 0th element is 0 degrees) - add 0.5 and cast as an int for the index.
String StringEventBuf[3] = {"Output String Buffer Event 1", "Output String Buffer Event 2", "Output String Buffer Event 3"}; String StringEventBuf[3] = {"Output String Buffer Event 1", "Output String Buffer Event 2", "Output String Buffer Event 3"};
long EventTimestamp[3] = {0, 0, 0}; int EventTimeStamp[3] = {0, 0, 0};
int EventTimeStampOld[3] = {0, 0, 0};
float Accel[3]; //Accelerometer array, 0 is X, 1 is Y and 2 is Z. float Accel[3]; //Accelerometer array, 0 is X, 1 is Y and 2 is Z.
unsigned long timeX = 0; unsigned long timeX = 0;
unsigned long oldtime = 0; unsigned long oldtime = 0;
void setup() { void setup() {
clearI2C();
// Issue 20 I2C clocks to make sure no slaves are hung in a read
pinMode(20, OUTPUT);
pinMode(21, OUTPUT);
pinMode(70, OUTPUT);
pinMode(71, OUTPUT);
digitalWrite(20, LOW);
digitalWrite(70, LOW);
for (int i = 0; i < 20; i++)
{
digitalWrite(21, LOW);
digitalWrite(71, LOW);
delayMicroseconds(10);
digitalWrite(21, HIGH);
digitalWrite(71, HIGH);
delayMicroseconds(10);
}
//Start Wire (I2C comms) //Start Wire (I2C comms)
@ -139,7 +124,7 @@ void setup() {
ThresholdSet(255, 255); ThresholdSet(255, 255);
//debug output //debug output
Serial.begin(9600);//we run the serial at 9600 for debugging only. Serial.begin(9600);//we run the serial at 9600 for debugging only and 115200 when we need to get more data out
PressureSetup(); PressureSetup();
Serial.println("Temp:"); Serial.println("Temp:");
@ -164,10 +149,10 @@ void setup() {
//now we set the HV bias //now we set the HV bias
VoltageSetPoint = (HVSetpoints[int(PressureTemp() + 0.5)] - 7); VoltageSetPoint = (HVSetpoints[int(PressureTemp() + 0.5)] - VoltageOffset);
VbiasSet(VoltageSetPoint);//VoltageSetPoint); VbiasSet(VoltageSetPoint);//VoltageSetPoint);
OutputFlag = false; OutputFlag = false;
delay(1000); //wait for the GPS to start up delay(10000); //wait for the GPS to start up
TimerInit(); TimerInit();
@ -240,52 +225,27 @@ void setup() {
Serial.print("VoltageSetPoint"); Serial.print("VoltageSetPoint");
Serial.print("; "); Serial.print("; ");
Serial.print("Ch1SetPoint"); Serial.print("PressureTempVal");
Serial.print("; "); Serial.print("; ");
Serial.print("Ch2SetPoint");
Serial.print("; ");
Serial.print("EventsLastSecond"); Serial.print("EventsLastSecond");
Serial.print("; "); Serial.print("; ");
Serial.print("EventRate");
Serial.print("; ");
Serial.print("CMFEvents"); Serial.print("CMFEvents");
Serial.print("; "); Serial.print("; ");
Serial.print("PPSUptime"); Serial.print("PPSUptime");
Serial.print("; ");
Serial.print("PPSLength");
Serial.print("; ");
Serial.print("EventTimeStampOld[0]");
Serial.print("; ");
Serial.print("EventTimeStampOld[1]");
Serial.print("; ");
Serial.print("EventTimeStampOld[2]");
Serial.println("; "); Serial.println("; ");
ThresholdSet(Ch1SetPoint, Ch2SetPoint);
} }
void loop() { void loop() {
ThresholdSet(Ch1SetPoint, Ch2SetPoint);
//Serial.println(Ch1SetPoint);
//Serial.println(Ch2SetPoint);
//Serial.println(Ch2SetPoint);
/* int Ch1 = analogRead(A1);
int Ch2 = analogRead(A2);
int ChA = analogRead(A6);
int ChB = analogRead(A7);
Serial.print(Ch1);
Serial.print(" ");
Serial.print(Ch2);
Serial.print(" ");
Serial.print(ChA);
Serial.print(" ");
Serial.println(ChB);
*/
PressureTempVal = PressureTemp();
//Serial.println(PressureTempVal);
//Serial.println(PressureTempValOld);
if (int(PressureTempValOld+0.5) != int(PressureTempVal+0.5))
{
VoltageSetPoint = (HVSetpoints[int(PressureTempVal + 0.5)] - 7);
VbiasSet(VoltageSetPoint);//VoltageSetPoint);
delay(1000);
Serial.println("changed voltage");
Serial.println(PressureTempVal);
Serial.println(PressureTempValOld);
PressureTempValOld = PressureTempVal;
}
@ -312,25 +272,66 @@ void loop() {
*/ */
Serial.print(VoltageSetPoint); Serial.print(VoltageSetPoint);
Serial.print("; "); Serial.print("; ");
Serial.print(Ch1SetPoint); Serial.print(PressureTempVal);
Serial.print("; "); Serial.print("; ");
Serial.print(Ch2SetPoint);
Serial.print("; ");
Serial.print(EventsLastSecond); Serial.print(EventsLastSecond);
Serial.print("; "); Serial.print("; ");
Serial.print(EventRate); //Serial.print(EventRate);
Serial.print("; "); //Serial.print("; ");
Serial.print(CMFEvents); Serial.print(CMFEvents);
Serial.print("; "); Serial.print("; ");
Serial.print(PPSUptime); Serial.print(PPSUptime);
Serial.print("; "); Serial.print("; ");
Serial.print(PPSLength);
Serial.print("; ");
Serial.print(float(CMFEvents)/float(PPSUptime)); Serial.print(float(CMFEvents)/float(PPSUptime));
Serial.print("; ");
Serial.print(EventTimeStampOld[0]);
Serial.print("; ");
Serial.print(EventTimeStampOld[1]);
Serial.print("; ");
Serial.print(EventTimeStampOld[2]);
Serial.println("; "); Serial.println("; ");
Checktemp = true;
OutputFlag = false; OutputFlag = false;
} }
//Serial.println(Ch1SetPoint);
//Serial.println(Ch2SetPoint);
//Serial.println(Ch2SetPoint);
/* int Ch1 = analogRead(A1);
int Ch2 = analogRead(A2);
int ChA = analogRead(A6);
int ChB = analogRead(A7);
Serial.print(Ch1);
Serial.print(" ");
Serial.print(Ch2);
Serial.print(" ");
Serial.print(ChA);
Serial.print(" ");
Serial.println(ChB);
*/
if ((PPSUptime % 60 == 0) & Checktemp) {
PressureTempVal = PressureTemp();
//Serial.println(PressureTempVal);
//Serial.println(PressureTempValOld);
if (int(PressureTempValOld+0.5) != int(PressureTempVal+0.5))
{
VoltageSetPoint = (HVSetpoints[int(PressureTempVal + 0.5)] - VoltageOffset);
VbiasSet(VoltageSetPoint);//VoltageSetPoint);
//delay(1000);
//Serial.println("changed voltage");
//Serial.println(PressureTempVal);
//Serial.println(PressureTempValOld);
PressureTempValOld = PressureTempVal;
}
Checktemp = false;
}
//else //else
//{ //{
// Serial.println("Nothing"); // Serial.println("Nothing");
@ -415,16 +416,20 @@ void TimerInit() {
void TC0_Handler() { void TC0_Handler() {
//This is called the one second event interrupt in documentation //This is called the one second event interrupt in documentation
//when the PPS event occurs //when the PPS event occurs
TC2->TC_CHANNEL[0].TC_CCR = TC_CCR_SWTRG; //forward the reset to TC2 event counter
PPSLength = TC0->TC_CHANNEL[0].TC_RA; // Read the RA reg (PPS period) PPSLength = TC0->TC_CHANNEL[0].TC_RA; // Read the RA reg (PPS period)
//Ch2SetPoint--; //Ch2SetPoint--;
if (EventsThisSecond > 2) if (EventsThisSecond > 3)
{ {
EventsThisSecond = 1; //we consider that >1 events is bounce, not events, this is <3% probable EventsThisSecond = 1; //we consider that >1 events is bounce, not events, this is <3% probable
} }
EventsLastSecond = EventsThisSecond; EventsLastSecond = EventsThisSecond;
CMFEvents = CMFEvents + EventsLastSecond; CMFEvents = CMFEvents + EventsLastSecond;
EventRate = (EventRate + EventsLastSecond) / 2; EventRate = (EventRate + EventsLastSecond) / 2;
OutputFlag = true; EventTimeStampOld[0]= EventTimeStamp[0];
EventTimeStampOld[1]= EventTimeStamp[1];
EventTimeStampOld[2]= EventTimeStamp[2];
OutputFlag = true;
//} //}
//else //else
//{ //{
@ -432,9 +437,15 @@ void TC0_Handler() {
//} //}
EventsThisSecond = 0; EventsThisSecond = 0;
EventTimeStamp[0] = 0;
EventTimeStamp[1] = 0;
EventTimeStamp[2] = 0;
digitalWrite(Event_LED, 0); digitalWrite(Event_LED, 0);
TC_GetStatus(TC0, 0); // Read status and clear load bits TC_GetStatus(TC0, 0); // Read status and clear load bits
TC_GetStatus(TC2, 0); // Reset TC2 at the same time
tempflipvar = !tempflipvar; tempflipvar = !tempflipvar;
digitalWrite(Power_LED, tempflipvar); digitalWrite(Power_LED, tempflipvar);
@ -446,12 +457,17 @@ void TC0_Handler() {
void TC6_Handler() { void TC6_Handler() {
//This is called when the trigger is activated //This is called when the trigger is activated
//EventTimestamp[EventsThisSecond] = TC0->TC_CHANNEL[0].TC_RA; //read the main clock and copy it to the event register int EventTimeBuffer = TC2->TC_CHANNEL[0].TC_RA;
//exlcude events if they happen within the dead time of 600 cycles (approx 18uS)
if (EventTimeBuffer > 800)
{
EventTimeStamp[EventsThisSecond] = EventTimeBuffer; //read the main clock and copy it to the event register
EventsThisSecond++; //increment the event counter for this second EventsThisSecond++; //increment the event counter for this second
digitalWrite(Event_LED, 1); digitalWrite(Event_LED, 1);
TC_GetStatus(TC2, 0); // Read status clear load bits, unlocking this interrupt. TC_GetStatus(TC2, 0); // Read status clear load bits, unlocking this interrupt.
}
} }

View file

@ -79,6 +79,8 @@ void PressureSetup(){
} }
float PressureTemp(){ float PressureTemp(){
//clearI2C();
//Read the Temperature using the pressure sensor, borrowed from the LPS Library by ryantm //Read the Temperature using the pressure sensor, borrowed from the LPS Library by ryantm
// go find the full thing at https://github.com/pololu/lps-arduino // go find the full thing at https://github.com/pololu/lps-arduino
Wire1.beginTransmission(PressureAddr); Wire1.beginTransmission(PressureAddr);
@ -95,6 +97,7 @@ float PressureTemp(){
// combine bytes // combine bytes
int16_t PresTempRaw = (th << 8 | tl); int16_t PresTempRaw = (th << 8 | tl);
return 42.5 + (float)PresTempRaw / 480; return 42.5 + (float)PresTempRaw / 480;
//clearI2C();
} }

View file

@ -158,3 +158,23 @@ void busscan1()
delay(5000); // wait 5 seconds for next scan delay(5000); // wait 5 seconds for next scan
} }
void clearI2C(){
// Issue 20 I2C clocks to make sure no slaves are hung in a read
pinMode(20, OUTPUT);
pinMode(21, OUTPUT);
pinMode(70, OUTPUT);
pinMode(71, OUTPUT);
digitalWrite(20, LOW);
digitalWrite(70, LOW);
for (int i = 0; i < 20; i++)
{
digitalWrite(21, LOW);
digitalWrite(71, LOW);
delayMicroseconds(10);
digitalWrite(21, HIGH);
digitalWrite(71, HIGH);
delayMicroseconds(10);
}
}