Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rate.w 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. @** A Rate of Change Indicator.
  2. \noindent A common metric used for understanding roast profiles is the rate of
  3. temperature change over a given amount of time. When roasters discuss roast
  4. profiles it is not uncommon to hear references to the change in temperature per
  5. 30 seconds or per minute, often with the sloppy shorthand $\Delta$ or with the
  6. term Rate of Rise (RoR). This is most commonly calculated from the secant line
  7. defined by two measurement points at the desired separation, however this may
  8. not be the most useful way to calculate this value.
  9. The rate of change can be considered as its own data series which happens to be
  10. derived from a primary measurement series. The interface for producing this
  11. series can sensibly match other classes which store, forward, or manipulate
  12. measurement data.
  13. @<Class declarations@>=
  14. class RateOfChange : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. RateOfChange(int cachetime = 1, int scaletime = 1);
  19. public slots:
  20. void newMeasurement(Measurement measure);
  21. void setCacheTime(int seconds);
  22. void setScaleTime(int seconds);
  23. signals:
  24. void newData(Measurement measure);
  25. private:
  26. int ct;
  27. int st;
  28. QList<Measurement> cache;
  29. QMap<double,double> smoothCache;
  30. };
  31. @ The interesting part of this class is in the |newMeasurement()| method. This
  32. is a slot method that will be called for every new measurement in the primary
  33. series. We require at least two measurements before calculating a rate of
  34. temperature change.
  35. @<RateOfChange implementation@>=
  36. void RateOfChange::newMeasurement(Measurement measure)
  37. {
  38. cache.append(measure);
  39. @<Remove stale measurements from rate cache@>@;
  40. if(cache.size() >= 2)
  41. {
  42. @<Calculate rate of change@>@;
  43. }
  44. }
  45. @ To calculate the rate of temperature change we require at least two cached
  46. measurements. Using only the most recent two measurements will result in a
  47. highly volatile rate of change while using two data points that are more
  48. separated will smooth out random fluctuations but provide a less immediate
  49. response to a change in the rate of change. For this reason we provide two
  50. parameters that can be adjusted independently: the amount of time we allow a
  51. measurement to stay in the cache determines how far apart the measurements
  52. used to calculate the rate of change are while a separate scale time is used
  53. to determine how the calculated value is presented. We never allow fewer than
  54. two cached values, but we can force the most volatile calculation by setting
  55. the cache time to 0 seconds.
  56. @<Remove stale measurements from rate cache@>=
  57. if(cache.size() > 2)
  58. {
  59. bool done = false;
  60. while(!done)
  61. {
  62. if(cache.front().time().secsTo(cache.back().time()) > ct)
  63. {
  64. cache.removeFirst();
  65. }
  66. else
  67. {
  68. done = true;
  69. }
  70. if(cache.size() < 3)
  71. {
  72. done = true;
  73. }
  74. }
  75. }
  76. @ The calculation method here is subject to change as this is still noisier
  77. than I would like. What we are doing here is calculating the rate of change
  78. between each pair of adjacent measurements in the cache and averaging them to
  79. produce something that is a little less noisy than just using the first and
  80. last measurements in the cache. Other techniques may be useful for reducing the
  81. noise further.
  82. The measurement will carry the fact that it is a relative measurement.
  83. @<Calculate rate of change@>=
  84. QList<double> rates;
  85. for(int i = 1; i < cache.size(); i++)
  86. {
  87. double mdiff = cache.at(i).temperature() - cache.at(i-1).temperature();
  88. double tdiff = (double)(cache.at(i-1).time().msecsTo(cache.at(i).time())) / 1000.0;
  89. rates.append(mdiff/tdiff);
  90. }
  91. double acc = 0.0;
  92. for(int i = 0; i < rates.size(); i++)
  93. {
  94. acc += rates.at(i);
  95. }
  96. double pavg = acc /= rates.size();
  97. double v2 = pavg * st;
  98. Measurement value(v2, cache.back().time(), cache.back().scale());
  99. value.insert("relative", true);
  100. emit newData(value);
  101. @ The rest of the class implementation is trivial.
  102. @<RateOfChange implementation@>=
  103. RateOfChange::RateOfChange(int cachetime, int scaletime) : ct(cachetime), st(1)
  104. {
  105. setScaleTime(scaletime);
  106. }
  107. void RateOfChange::setCacheTime(int seconds)
  108. {
  109. ct = seconds;
  110. }
  111. void RateOfChange::setScaleTime(int seconds)
  112. {
  113. st = (seconds > 0 ? seconds : 1);
  114. }
  115. @ This is exposed to the host environment in the usual way.
  116. @<Function prototypes for scripting@>=
  117. QScriptValue constructRateOfChange(QScriptContext *context, QScriptEngine *engine);
  118. void setRateOfChangeProperties(QScriptValue value, QScriptEngine *engine);
  119. @ The constructor is registered with the scripting engine.
  120. @<Set up the scripting engine@>=
  121. constructor = engine->newFunction(constructRateOfChange);
  122. value = engine->newQMetaObject(&RateOfChange::staticMetaObject, constructor);
  123. engine->globalObject().setProperty("RateOfChange", value);
  124. @ The constructor takes two arguments if they are provided.
  125. @<Functions for scripting@>=
  126. QScriptValue constructRateOfChange(QScriptContext *context, QScriptEngine *engine)
  127. {
  128. int cachetime = 1;
  129. int scaletime = 1;
  130. if(context->argumentCount() > 0)
  131. {
  132. cachetime = argument<int>(0, context);
  133. if(context->argumentCount() > 1)
  134. {
  135. scaletime = argument<int>(1, context);
  136. }
  137. }
  138. QScriptValue object = engine->newQObject(new RateOfChange(cachetime, scaletime));
  139. setRateOfChangeProperties(object, engine);
  140. return object;
  141. }
  142. void setRateOfChangeProperties(QScriptValue value, QScriptEngine *engine)
  143. {
  144. setQObjectProperties(value, engine);
  145. }
  146. @ To make use of this feature conveniently, we must integrate this with the
  147. in-program configuration system by providing a configuration widget.
  148. @<Class declarations@>=
  149. class RateOfChangeConfWidget : public BasicDeviceConfigurationWidget
  150. {
  151. Q_OBJECT
  152. public:
  153. Q_INVOKABLE RateOfChangeConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  154. private slots:
  155. void updateColumn(const QString &column);
  156. void updateCacheTime(const QString &seconds);
  157. void updateScaleTime(const QString &seconds);
  158. };
  159. @ The constructor sets up the user interface.
  160. @<RateOfChangeConfWidget implementation@>=
  161. RateOfChangeConfWidget::RateOfChangeConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  162. : BasicDeviceConfigurationWidget(model, index)
  163. {
  164. QFormLayout *layout = new QFormLayout;
  165. QLineEdit *column = new QLineEdit;
  166. layout->addRow(tr("Primary series column name:"), column);
  167. QSpinBox *cacheTime = new QSpinBox;
  168. cacheTime->setMinimum(0);
  169. cacheTime->setMaximum(300);
  170. layout->addRow(tr("Cache time:"), cacheTime);
  171. QSpinBox *scaleTime = new QSpinBox;
  172. scaleTime->setMinimum(1);
  173. scaleTime->setMaximum(300);
  174. layout->addRow(tr("Scale time:"), scaleTime);
  175. @<Get device configuration data for current node@>@;
  176. for(int i = 0; i < configData.size(); i++)
  177. {
  178. node = configData.at(i).toElement();
  179. if(node.attribute("name") == "column")
  180. {
  181. column->setText(node.attribute("value"));
  182. }
  183. else if(node.attribute("name") == "cache")
  184. {
  185. cacheTime->setValue(node.attribute("value").toInt());
  186. }
  187. else if(node.attribute("name") == "scale")
  188. {
  189. scaleTime->setValue(node.attribute("value").toInt());
  190. }
  191. }
  192. updateColumn(column->text());
  193. updateCacheTime(cacheTime->text());
  194. updateScaleTime(scaleTime->text());
  195. connect(column, SIGNAL(textEdited(QString)), this, SLOT(updateColumn(QString)));
  196. connect(cacheTime, SIGNAL(valueChanged(QString)), this, SLOT(updateCacheTime(QString)));
  197. connect(scaleTime, SIGNAL(valueChanged(QString)), this, SLOT(updateScaleTime(QString)));
  198. setLayout(layout);
  199. }
  200. @ A set of update methods modify the device configuration to reflect changes in
  201. the configuration widget.
  202. @<RateOfChangeConfWidget implementation@>=
  203. void RateOfChangeConfWidget::updateColumn(const QString &column)
  204. {
  205. updateAttribute("column", column);
  206. }
  207. void RateOfChangeConfWidget::updateCacheTime(const QString &seconds)
  208. {
  209. updateAttribute("cache", seconds);
  210. }
  211. void RateOfChangeConfWidget::updateScaleTime(const QString &seconds)
  212. {
  213. updateAttribute("scale", seconds);
  214. }
  215. @ This is registered with the configuration system.
  216. @<Register device configuration widgets@>=
  217. app.registerDeviceConfigurationWidget("rate", RateOfChangeConfWidget::staticMetaObject);
  218. @ This is accessed through the advanced features menu.
  219. @<Add node inserters to advanced features menu@>=
  220. NodeInserter *rateOfChangeInserter = new NodeInserter(tr("Rate of Change"), tr("Rate of Change"), "rate");
  221. connect(rateOfChangeInserter, SIGNAL(triggered(QString, QString)), this, SLOT(insertChildNode(QString, QString)));
  222. advancedMenu->addAction(rateOfChangeInserter);
  223. @ Our class implementation is currently expanded into |"typica.cpp"|.
  224. @<Class implementations@>=
  225. @<RateOfChangeConfWidget implementation@>