Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1.  
  2.  
  3. /*
  4. * Include Files
  5. *
  6. */
  7. #if defined(MATLAB_MEX_FILE)
  8. #include "tmwtypes.h"
  9. #include "simstruc_types.h"
  10. #else
  11. #include "rtwtypes.h"
  12. #endif
  13.  
  14. /* %%%-SFUNWIZ_wrapper_includes_Changes_BEGIN --- EDIT HERE TO _END */
  15. #include <math.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sstream>
  23. #include <iostream>
  24.  
  25. #include <boost/lexical_cast.hpp>
  26. #include <boost/algorithm/string/split.hpp>
  27. #include <boost/algorithm/string.hpp>
  28.  
  29.  
  30. /* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */
  31. #define u_width 1
  32. #define y_width 1
  33. /*
  34. * Create external references here.
  35. *
  36. */
  37. /* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */
  38. /* extern double func(double a); */
  39.  
  40. int inline openOutputPipe(const std::string& outputName)
  41. {
  42. const int readFlags = O_WRONLY;
  43.  
  44. std::cout << "Waiting for a client to write..." << std::endl;
  45. int outputPipeFD = open(outputName.c_str(), readFlags);
  46. if (outputPipeFD < 0)
  47. {
  48. throw new std::runtime_error("Error: open( ): for Input Pipe ");
  49. }
  50.  
  51.  
  52.  
  53. return outputPipeFD;
  54. }
  55.  
  56. class GlobalWrapper
  57. {
  58. public:
  59. bool initialized;
  60. int inputPipe;
  61. int outputPipe;
  62. char inputBuffer[1024];
  63. GlobalWrapper() :
  64. initialized(false)
  65. {
  66. }
  67. };
  68.  
  69. GlobalWrapper gVars;
  70.  
  71. #undef MDL_START /* Change to #undef to remove function */
  72.  
  73. inline void init(const int processId)
  74. {
  75. if(!gVars.initialized)
  76. {
  77.  
  78. const int readFlags = O_RDONLY;
  79.  
  80. std::stringstream inputPipeNameSS;
  81. inputPipeNameSS << std::string("/home/bewo/Hiwi/geneial_strategy/build/output.") << processId << std::string(".fifo");
  82. const std::string inputPipeName = inputPipeNameSS.str();
  83.  
  84. std::cout << "Opening Input Pipe..." << std::endl;
  85. gVars.inputPipe = open(inputPipeName.c_str(),readFlags);
  86.  
  87. if(gVars.inputPipe < 0)
  88. {
  89. std::cout << "Error Creating Input Pipe." << std::endl;
  90. }
  91.  
  92.  
  93. std::stringstream outputPipeNameSS;
  94. outputPipeNameSS << std::string("/home/bewo/Hiwi/geneial_strategy/build/input.") << processId << std::string(".fifo");
  95. const std::string outputPipeName = outputPipeNameSS.str();
  96.  
  97. gVars.outputPipe= openOutputPipe(outputPipeName);
  98.  
  99.  
  100. //Initialization
  101.  
  102. gVars.initialized = true;
  103.  
  104.  
  105. }
  106.  
  107. }
  108.  
  109.  
  110.  
  111. /* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */
  112.  
  113. /*
  114. * Output functions
  115. *
  116. */
  117. void ga_strategy_01_Outputs_wrapper(const real_T *t_req_act,
  118. const real_T *wheel_ang_vel,
  119. const real_T *gear_ratio,
  120. const real_T *velocity,
  121. const real_T *fuel_cons_abs,
  122. const real_T *soc,
  123. const real_T *fuel_consumption,
  124. const real_T *time,
  125. real_T *split)
  126. {
  127. /* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
  128.  
  129. const int processId = 1;
  130.  
  131. init(processId); //TODO (bewo): Get ProcessId from Matlab input?
  132. assert(gVars.initialized);
  133.  
  134. std::stringstream ss;
  135. const std::string delimiter(";");
  136.  
  137. ss << *t_req_act << delimiter
  138. << *wheel_ang_vel << delimiter
  139. << *gear_ratio << delimiter
  140. << *velocity << delimiter
  141. << *fuel_cons_abs << delimiter
  142. << *soc << delimiter
  143. << *fuel_consumption << delimiter
  144. << *time;
  145. const auto output = ss.str();
  146. const char* rawStr =output.c_str();
  147.  
  148.  
  149. std::cout <<"Open Pipe " << std::endl;
  150. write(gVars.outputPipe, rawStr, strlen(rawStr));
  151.  
  152. std::cout <<"inputPipe" << gVars.inputPipe << std::endl;
  153.  
  154. int bytes = read(gVars.inputPipe, gVars.inputBuffer, 1023);
  155. std::cout << "Read " << bytes << std::endl;
  156. gVars.inputBuffer[bytes] = '\0';
  157.  
  158. std::cout <<"Read:" << gVars.inputBuffer <<std::endl;
  159. std::vector<std::string> numbers;
  160. std::vector<double> outputs;
  161.  
  162.  
  163.  
  164. boost::split(numbers, gVars.inputBuffer, boost::is_any_of(";"));
  165.  
  166. for (const auto & number : numbers) {
  167. //std::cout << "Converting " << number << std::endl;
  168. try
  169. {
  170. outputs.push_back(boost::lexical_cast<double>(number));
  171. }
  172. catch(const boost::bad_lexical_cast &)
  173. {
  174. std::cout << "Cannot Convert " << number << std::endl;
  175. }
  176. }
  177.  
  178. if(outputs.size() != 1)
  179. {
  180. std::cout << "Was expecting 1 values, got " << outputs.size() << std::endl;
  181. }
  182.  
  183. *split = outputs[0];
  184.  
  185. /* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement