Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ConfigurationHandler/Controls/SetpointControl.cpp@51:66b820f203a5, 2016-09-13 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Sep 13 21:29:24 2016 +0000
- Revision:
- 51:66b820f203a5
- Parent:
- 46:4cb96ab2d1c8
- Child:
- 56:225786c56315
committing;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 14:cc916fa8dd11 | 1 | /****************************************************************************** |
jmarkel44 | 14:cc916fa8dd11 | 2 | * |
jmarkel44 | 19:9bc8fabeddfa | 3 | * File: SetpointControl.cpp |
jmarkel44 | 20:653923c2f37a | 4 | * Desciption: ICE Setpoint Control Class implementation |
jmarkel44 | 14:cc916fa8dd11 | 5 | * |
jmarkel44 | 14:cc916fa8dd11 | 6 | *****************************************************************************/ |
jmarkel44 | 13:c80c283f9db2 | 7 | #include "SetpointControl.h" |
jmarkel44 | 14:cc916fa8dd11 | 8 | #include "mDot.h" |
jmarkel44 | 20:653923c2f37a | 9 | #include "MbedJSONValue.h" |
jmarkel44 | 51:66b820f203a5 | 10 | #include "global.h" |
jmarkel44 | 28:c410a61238bb | 11 | #include <string> |
jmarkel44 | 13:c80c283f9db2 | 12 | |
jmarkel44 | 14:cc916fa8dd11 | 13 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 14:cc916fa8dd11 | 14 | |
jmarkel44 | 28:c410a61238bb | 15 | bool SetpointControl::load(string _controlFile) |
jmarkel44 | 20:653923c2f37a | 16 | { |
jmarkel44 | 51:66b820f203a5 | 17 | MbedJSONValue json_value; // JSON parsing element |
jmarkel44 | 51:66b820f203a5 | 18 | controlFile = _controlFile; |
jmarkel44 | 51:66b820f203a5 | 19 | |
jmarkel44 | 51:66b820f203a5 | 20 | // open and read from the control file |
jmarkel44 | 14:cc916fa8dd11 | 21 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY); |
jmarkel44 | 51:66b820f203a5 | 22 | if ( file.fd < 0 ) |
jmarkel44 | 28:c410a61238bb | 23 | return false; |
jmarkel44 | 20:653923c2f37a | 24 | |
jmarkel44 | 20:653923c2f37a | 25 | // read the data into a buffer |
jmarkel44 | 28:c410a61238bb | 26 | char dataBuf[1024]; |
jmarkel44 | 28:c410a61238bb | 27 | |
jmarkel44 | 28:c410a61238bb | 28 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 28:c410a61238bb | 29 | if ( bytes_read != sizeof(dataBuf) ) { |
jmarkel44 | 28:c410a61238bb | 30 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
jmarkel44 | 46:4cb96ab2d1c8 | 31 | // we can't throw exceptions in mbed, so just return false. the calling function will |
jmarkel44 | 51:66b820f203a5 | 32 | // destroy the object |
jmarkel44 | 28:c410a61238bb | 33 | return false; |
jmarkel44 | 28:c410a61238bb | 34 | } |
jmarkel44 | 51:66b820f203a5 | 35 | |
jmarkel44 | 51:66b820f203a5 | 36 | // close the file |
jmarkel44 | 28:c410a61238bb | 37 | GLOBAL_mdot->closeUserFile(file); |
jmarkel44 | 28:c410a61238bb | 38 | |
jmarkel44 | 51:66b820f203a5 | 39 | // parse the json data |
jmarkel44 | 28:c410a61238bb | 40 | parse(json_value, dataBuf); |
jmarkel44 | 51:66b820f203a5 | 41 | |
jmarkel44 | 28:c410a61238bb | 42 | id = json_value["id"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 43 | name = json_value["name"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 44 | priority = atoi(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 45 | input = json_value["input"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 46 | output = json_value["output"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 47 | productFactor = atof(json_value["prodfact"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 48 | highAlert = atof(json_value["halert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 49 | lowAlert = atof(json_value["lalert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 50 | highFailsafe = atof(json_value["hfs"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 51 | lowFailsafe = atof(json_value["lfs"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 52 | tol = atof(json_value["tol"].get<string>().c_str()); |
jmarkel44 | 20:653923c2f37a | 53 | |
jmarkel44 | 51:66b820f203a5 | 54 | return true; // object created successfully |
jmarkel44 | 51:66b820f203a5 | 55 | } |
jmarkel44 | 51:66b820f203a5 | 56 | |
jmarkel44 | 51:66b820f203a5 | 57 | void SetpointControl::registerControl(void) |
jmarkel44 | 51:66b820f203a5 | 58 | { |
jmarkel44 | 51:66b820f203a5 | 59 | if ( GLOBAL_outputTask_thread ) { |
jmarkel44 | 51:66b820f203a5 | 60 | // register our priority with the output master |
jmarkel44 | 51:66b820f203a5 | 61 | } |
jmarkel44 | 51:66b820f203a5 | 62 | } |
jmarkel44 | 51:66b820f203a5 | 63 | |
jmarkel44 | 51:66b820f203a5 | 64 | void SetpointControl::start(void) |
jmarkel44 | 51:66b820f203a5 | 65 | { |
jmarkel44 | 51:66b820f203a5 | 66 | // this is the initial state, let's determine what needs to be done |
jmarkel44 | 51:66b820f203a5 | 67 | this->currentState = STATE_STARTUP; |
jmarkel44 | 19:9bc8fabeddfa | 68 | } |
jmarkel44 | 19:9bc8fabeddfa | 69 | |
jmarkel44 | 51:66b820f203a5 | 70 | void SetpointControl::update(void) |
jmarkel44 | 51:66b820f203a5 | 71 | { |
jmarkel44 | 51:66b820f203a5 | 72 | printf("\r%s is working on %s\n", __func__, controlFile.c_str()); |
jmarkel44 | 51:66b820f203a5 | 73 | switch (this->currentState) { |
jmarkel44 | 51:66b820f203a5 | 74 | case STATE_STARTUP: |
jmarkel44 | 51:66b820f203a5 | 75 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 76 | // start the feed right away |
jmarkel44 | 51:66b820f203a5 | 77 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 78 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 79 | } else { |
jmarkel44 | 51:66b820f203a5 | 80 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 81 | } |
jmarkel44 | 51:66b820f203a5 | 82 | break; |
jmarkel44 | 51:66b820f203a5 | 83 | case STATE_CONTROL_ON: |
jmarkel44 | 51:66b820f203a5 | 84 | if ( this->overLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 85 | // stop the feed |
jmarkel44 | 51:66b820f203a5 | 86 | this->stopFeed(); |
jmarkel44 | 51:66b820f203a5 | 87 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 88 | } else { |
jmarkel44 | 51:66b820f203a5 | 89 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 90 | } |
jmarkel44 | 51:66b820f203a5 | 91 | break; |
jmarkel44 | 51:66b820f203a5 | 92 | case STATE_CONTROL_OFF: |
jmarkel44 | 51:66b820f203a5 | 93 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 94 | // start the feed |
jmarkel44 | 51:66b820f203a5 | 95 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 96 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 97 | } else { |
jmarkel44 | 51:66b820f203a5 | 98 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 99 | } |
jmarkel44 | 51:66b820f203a5 | 100 | break; |
jmarkel44 | 51:66b820f203a5 | 101 | //case STATE_CONTROL_DISABLED: |
jmarkel44 | 51:66b820f203a5 | 102 | //case STATE_CONTROL_PAUSED: |
jmarkel44 | 51:66b820f203a5 | 103 | default: |
jmarkel44 | 51:66b820f203a5 | 104 | break; |
jmarkel44 | 51:66b820f203a5 | 105 | } |
jmarkel44 | 51:66b820f203a5 | 106 | } |
jmarkel44 | 51:66b820f203a5 | 107 | |
jmarkel44 | 51:66b820f203a5 | 108 | bool SetpointControl::overLimit(void) |
jmarkel44 | 51:66b820f203a5 | 109 | { |
jmarkel44 | 51:66b820f203a5 | 110 | return false; |
jmarkel44 | 51:66b820f203a5 | 111 | } |
jmarkel44 | 51:66b820f203a5 | 112 | |
jmarkel44 | 51:66b820f203a5 | 113 | bool SetpointControl::underLimit(void) |
jmarkel44 | 51:66b820f203a5 | 114 | { |
jmarkel44 | 51:66b820f203a5 | 115 | return false; |
jmarkel44 | 51:66b820f203a5 | 116 | } |
jmarkel44 | 51:66b820f203a5 | 117 | |
jmarkel44 | 51:66b820f203a5 | 118 | void SetpointControl::startFeed(void) |
jmarkel44 | 51:66b820f203a5 | 119 | { |
jmarkel44 | 51:66b820f203a5 | 120 | logInfo("%s attempting to start feed on relay %s\n", |
jmarkel44 | 51:66b820f203a5 | 121 | controlFile.c_str(), output.c_str()); |
jmarkel44 | 51:66b820f203a5 | 122 | |
jmarkel44 | 51:66b820f203a5 | 123 | // TODO: send a message to the output task to turn the relay ON |
jmarkel44 | 51:66b820f203a5 | 124 | } |
jmarkel44 | 51:66b820f203a5 | 125 | |
jmarkel44 | 51:66b820f203a5 | 126 | void SetpointControl::stopFeed(void) |
jmarkel44 | 51:66b820f203a5 | 127 | { |
jmarkel44 | 51:66b820f203a5 | 128 | logInfo("%s attempting to stop feed on relay %s\n", |
jmarkel44 | 51:66b820f203a5 | 129 | controlFile.c_str(), output.c_str()); |
jmarkel44 | 51:66b820f203a5 | 130 | // TODO: send a message to the output task to turn the relay OFF |
jmarkel44 | 51:66b820f203a5 | 131 | } |