Gammatech Posted October 5, 2009 Share Posted October 5, 2009 Hi I have three channels updated every second from a serial input. The first is the current Tonnes per hour from a weigher on the conveyor belt, the second is the instantaneous Ash content of the material on the belt as a percentage (0-100%) and the third is the instantaneous Moisture content of the material on the belt again as a percentage. My application needs to display and store the tonnage weighted average (Ash & Moisture) either for a specific period or a specific tonnage (user selected) I have all the user selection code working fine which sets a flag if accumulation into the average should occur. (BeginSeq(AccumulateToBatch) The sequence to accumulate each seconds data simply takes the TonnesThis Second (A declared Global variable) (TonnesPerHour/3600) and multiplies this by the current instantaneous quality (Ash, Moisture) percent to get the AshTonnesThisSecond and MoistureTonnesThisSecond. These are each accumulated to running totals called BatchAshTonnes and BatchMoistTonnes. A seperate accumulation of the TonnesThis Second is made into BatchTonnes. By dividing BatchAshTonnes by BatchTonnes I get the BatchAsh. (Similarly for moisture) This works fine. This sequence is called "AccumulateToBatch" // Sequence AccumulateToBatch TonnesThisSecond = TPH/3600 AshTonnesThisSec = TonnesThisSecond * Ash MoistTonnesThisSec = TonnesThisSecond * Moisture // May be belt and braces but just in case this is the first value accumulated If(IsEmpty(BatchAshTonnes)) VM1Val = AshTonnesThisSec Else VM1Val = BatchAshTonnes + AshTonnesThisSec EndIf // AshTonnesThis second will have indeterminate time so insert time before storage VM1Val = InsertTime(VM1Val,SysTime(),-1) BatchAshTonnes.AddValue(VM1Val) If(IsEmpty(BatchMoistTonnes)) VM1Val = MoistTonnesThisSec Else VM1Val = BatchMoistTonnes + MoistTonnesThisSec EndIf // MoistTonnesThis second will have indeterminate time so insert time before storage VM1Val = InsertTime(VM1Val,SysTime(),-1) BatchMoistTonnes.AddValue(VM1Val) If(IsEmpty(BatchTonnes)) VM1Val = TonnesThisSecond Else VM1Val = BatchTonnes + TonnesThisSecond EndIf // TonnesThis second will have indeterminate time so insert time before storage VM1Val = InsertTime(VM1Val,SysTime(),-1) BatchTonnes.AddValue(VM1Val) If(BatchTonnes > 0) VM1Val = BatchAshTonnes / BatchTonnes BatchAsh.AddValue(VM1Val) VM1Val = BatchMoistTonnes / BatchTonnes BatchMoisture.AddValue(VM1Val) Else VM1Val = 0 // VM1Val will have indeterminate time so insert time before storage VM1Val = InsertTime(VM1Val,SysTime(),-1) BatchAsh.AddValue(VM1Val) BatchMoisture.AddValue(VM1Val) EndIf At the end of the Batch I call a sequence to close the batch which stores the final tonnes accumulated into the batch (BatchTonnes), the BatchAsh and the BatchMoisture. This sequence also has to set the running totals, (BatchTonnes, BatchAshTonnes anf BatchMoistTonnes) to zero ready for the next batch. This sequence is called "CloseBatch" // Sequence CloseBatch. // Transfers the resultant BatchAsh, BatchMoisture and BatchTonnes to their respective 'Final' channels and resets them to zero // Also stores BatchStart and BatchEnd times in their 'Final' channels. It might be possible to use the .Time element of a Final channel rather than the BatchEndFinal store. but this seems clearer. BatchAshFinal.AddValue(BatchAsh) BatchMoistFinal.AddValue(BatchMoisture) BatchTonnesFinal.AddValue(BatchTonnes) BatchStartFinal.AddValue(BatchStart) BatchEndFinal.AddValue(BatchEnd) // reset accumulators to zero BatchAshTonnes.AddValue(0) BatchMoistTonnes.AddValue(0) BatchAsh.AddValue(0) BatchMoisture.AddValue(0) BatchTonnes.AddValue(0) The problem is that it seems the channels used for the accumulation don't always actually reset to zero properly (The BatchTonnes always does?) as some times the next batch has a high BatchAsh and or BatchMoisture shown. I can't see where I'm going wrong, any ideas? Martin Link to comment Share on other sites More sharing options...
This topic is now archived and is closed to further replies.