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.
Fork of OmniWheels by
SerialCom.cpp
00001 /* 00002 * SerialServer.cpp 00003 * Copyright (c) 2017, ZHAW 00004 * All rights reserved. 00005 * 00006 * Created on: 05.06.2017 00007 * Author: Marcel Honegger 00008 */ 00009 00010 00011 #include <mbed.h> 00012 #include <iostream> 00013 #include <string> 00014 #include <sstream> 00015 #include <vector> 00016 #include "SerialCom.h" 00017 00018 using namespace std; 00019 00020 const float SerialCom::PERIOD = 0.0001f; // the period of the timer interrupt, given in [s] 00021 const char SerialCom::DELIM = ','; 00022 00023 /** 00024 * Create a serial server object. 00025 00026 00027 00028 SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) { 00029 */ 00030 SerialCom::SerialCom(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 00031 // initialize local values 00032 00033 input.clear(); 00034 00035 // start thread and ticker timer interrupt service routine 00036 00037 thread.start(callback(this, &SerialCom::run)); 00038 ticker.attach(callback(this, &SerialCom::sendSignal), PERIOD); 00039 } 00040 00041 /** 00042 * Delete the serial server object and release all allocated resources. 00043 */ 00044 SerialCom::~SerialCom() { 00045 00046 ticker.detach(); 00047 } 00048 00049 /** 00050 * This method is called by the ticker timer interrupt service routine. 00051 * It sends a signal to the thread to make it run again. 00052 */ 00053 void SerialCom::sendSignal() { 00054 00055 thread.signal_set(signal); 00056 } 00057 00058 /** 00059 * This <code>run()</code> method contains an infinite loop with the run logic. 00060 */ 00061 void SerialCom::run() { 00062 00063 Serial pc(PA_2, PA_3); // tx, rx 00064 pc.baud(9600); 00065 00066 while (true) { 00067 00068 // wait for the periodic signal 00069 00070 thread.signal_wait(signal); 00071 00072 00073 00074 // read received characters while buffer is full 00075 00076 while (serial.readable()) { 00077 int32_t c = serial.getc(); 00078 //if (input.size() >= MESSAGE_SIZE) input.erase(0, 1); 00079 input += static_cast<uint8_t>(c); 00080 } 00081 00082 // try to decode a message 00083 00084 if (input[input.size()-1] != '&') { 00085 00086 // message is too short, keep reading... 00087 00088 } else { 00089 pc.printf("command: %s", input); 00090 splitString(); 00091 if (tokens[0] == "M") { 00092 if (tokens.size() == 45+2) { 00093 float newK[3][15]; 00094 int y = 0; 00095 int x = 0; 00096 float checksum = 0; 00097 for (int i=1; i < tokens.size()-1; i++) { 00098 newK[y][x] = strtof((tokens[i]).c_str(), 0); 00099 pc.printf("Element %d , %d: %.10f\r\n", y, x, newK[y][x]); 00100 x++; 00101 checksum += newK[y][x]; 00102 if (x == 15){ 00103 x = 0; 00104 y++; 00105 } 00106 } 00107 } 00108 else{ 00109 pc.printf("Data to short od long\r\n"); 00110 } 00111 00112 } 00113 else if (tokens[0] == "G") { 00114 pc.printf("Send data to pc\r\n"); 00115 if(sendData == false) { 00116 sendData = true; 00117 runCount = 0; 00118 } 00119 else{ 00120 sendData = false; 00121 } 00122 00123 } 00124 else{ 00125 pc.printf("Command not understood\r\n"); 00126 } 00127 00128 00129 tokens.clear(); 00130 input.clear(); 00131 00132 00133 } 00134 if(sendData == true) { 00135 if (output.size() == 0 && runCount >= 1000){ 00136 output += "Hello nicolas\n"; 00137 runCount = 0; 00138 } 00139 } 00140 runCount++; 00141 while (serial.writeable() && (output.size() > 0)) { 00142 serial.putc(output[0]); 00143 output.erase(0, 1); 00144 } 00145 00146 } 00147 00148 } 00149 00150 00151 void SerialCom::splitString() { 00152 stringstream ss(input); 00153 string item; 00154 while (getline(ss, item, DELIM)) { 00155 tokens.push_back(item); 00156 } 00157 return; 00158 }
Generated on Fri Jul 22 2022 04:54:00 by
