BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /*
borlanic 0:fbdae7e6d805 2 * SerialServer.cpp
borlanic 0:fbdae7e6d805 3 * Copyright (c) 2017, ZHAW
borlanic 0:fbdae7e6d805 4 * All rights reserved.
borlanic 0:fbdae7e6d805 5 *
borlanic 0:fbdae7e6d805 6 * Created on: 05.06.2017
borlanic 0:fbdae7e6d805 7 * Author: Marcel Honegger
borlanic 0:fbdae7e6d805 8 */
borlanic 0:fbdae7e6d805 9
borlanic 0:fbdae7e6d805 10
borlanic 0:fbdae7e6d805 11 #include <mbed.h>
borlanic 0:fbdae7e6d805 12 #include <iostream>
borlanic 0:fbdae7e6d805 13 #include <string>
borlanic 0:fbdae7e6d805 14 #include <sstream>
borlanic 0:fbdae7e6d805 15 #include <vector>
borlanic 0:fbdae7e6d805 16 #include "SerialCom.h"
borlanic 0:fbdae7e6d805 17
borlanic 0:fbdae7e6d805 18 using namespace std;
borlanic 0:fbdae7e6d805 19
borlanic 0:fbdae7e6d805 20 const float SerialCom::PERIOD = 0.0001f; // the period of the timer interrupt, given in [s]
borlanic 0:fbdae7e6d805 21 const char SerialCom::DELIM = ',';
borlanic 0:fbdae7e6d805 22
borlanic 0:fbdae7e6d805 23 /**
borlanic 0:fbdae7e6d805 24 * Create a serial server object.
borlanic 0:fbdae7e6d805 25
borlanic 0:fbdae7e6d805 26
borlanic 0:fbdae7e6d805 27
borlanic 0:fbdae7e6d805 28 SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) {
borlanic 0:fbdae7e6d805 29 */
borlanic 0:fbdae7e6d805 30 SerialCom::SerialCom(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) {
borlanic 0:fbdae7e6d805 31 // initialize local values
borlanic 0:fbdae7e6d805 32
borlanic 0:fbdae7e6d805 33 input.clear();
borlanic 0:fbdae7e6d805 34
borlanic 0:fbdae7e6d805 35 // start thread and ticker timer interrupt service routine
borlanic 0:fbdae7e6d805 36 dataRecived = false;
borlanic 0:fbdae7e6d805 37
borlanic 0:fbdae7e6d805 38 gainG = 1.1f;
borlanic 0:fbdae7e6d805 39 gain_dG = 1.1f;
borlanic 0:fbdae7e6d805 40
borlanic 0:fbdae7e6d805 41 thread.start(callback(this, &SerialCom::run));
borlanic 0:fbdae7e6d805 42 ticker.attach(callback(this, &SerialCom::sendSignal), PERIOD);
borlanic 0:fbdae7e6d805 43 }
borlanic 0:fbdae7e6d805 44
borlanic 0:fbdae7e6d805 45 /**
borlanic 0:fbdae7e6d805 46 * Delete the serial server object and release all allocated resources.
borlanic 0:fbdae7e6d805 47 */
borlanic 0:fbdae7e6d805 48 SerialCom::~SerialCom() {
borlanic 0:fbdae7e6d805 49
borlanic 0:fbdae7e6d805 50 ticker.detach();
borlanic 0:fbdae7e6d805 51 }
borlanic 0:fbdae7e6d805 52
borlanic 0:fbdae7e6d805 53 /**
borlanic 0:fbdae7e6d805 54 * This method is called by the ticker timer interrupt service routine.
borlanic 0:fbdae7e6d805 55 * It sends a signal to the thread to make it run again.
borlanic 0:fbdae7e6d805 56 */
borlanic 0:fbdae7e6d805 57 void SerialCom::sendSignal() {
borlanic 0:fbdae7e6d805 58
borlanic 0:fbdae7e6d805 59 thread.signal_set(signal);
borlanic 0:fbdae7e6d805 60 }
borlanic 0:fbdae7e6d805 61
borlanic 0:fbdae7e6d805 62 /**
borlanic 0:fbdae7e6d805 63 * This <code>run()</code> method contains an infinite loop with the run logic.
borlanic 0:fbdae7e6d805 64 */
borlanic 0:fbdae7e6d805 65 void SerialCom::run() {
borlanic 0:fbdae7e6d805 66
borlanic 0:fbdae7e6d805 67 //Serial pc(PA_2, PA_3); // tx, rx
borlanic 0:fbdae7e6d805 68 //pc.baud(9600);
borlanic 0:fbdae7e6d805 69
borlanic 0:fbdae7e6d805 70 Serial pc1(USBTX, USBRX); // tx, rx
borlanic 0:fbdae7e6d805 71 pc1.baud(100000);
borlanic 0:fbdae7e6d805 72
borlanic 0:fbdae7e6d805 73 while (true) {
borlanic 0:fbdae7e6d805 74
borlanic 0:fbdae7e6d805 75 // wait for the periodic signal
borlanic 0:fbdae7e6d805 76
borlanic 0:fbdae7e6d805 77 thread.signal_wait(signal);
borlanic 0:fbdae7e6d805 78
borlanic 0:fbdae7e6d805 79
borlanic 0:fbdae7e6d805 80
borlanic 0:fbdae7e6d805 81 // read received characters while buffer is full
borlanic 0:fbdae7e6d805 82
borlanic 0:fbdae7e6d805 83 while (serial.readable()) {
borlanic 0:fbdae7e6d805 84 int32_t c = serial.getc();
borlanic 0:fbdae7e6d805 85 //if (input.size() >= MESSAGE_SIZE) input.erase(0, 1);
borlanic 0:fbdae7e6d805 86 input += static_cast<uint8_t>(c);
borlanic 0:fbdae7e6d805 87 }
borlanic 0:fbdae7e6d805 88
borlanic 0:fbdae7e6d805 89 // try to decode a message
borlanic 0:fbdae7e6d805 90
borlanic 0:fbdae7e6d805 91 if (input[input.size()-1] != '&') {
borlanic 0:fbdae7e6d805 92
borlanic 0:fbdae7e6d805 93 // message is too short, keep reading...
borlanic 0:fbdae7e6d805 94
borlanic 0:fbdae7e6d805 95 } else {
borlanic 0:fbdae7e6d805 96 //pc.printf("command: %s\r\n", input);
borlanic 0:fbdae7e6d805 97 splitString();
borlanic 0:fbdae7e6d805 98 if (tokens[0] == "M") {
borlanic 0:fbdae7e6d805 99 if (tokens.size() == 45+2) {
borlanic 0:fbdae7e6d805 100 float newK[3][15];
borlanic 0:fbdae7e6d805 101 int y = 0;
borlanic 0:fbdae7e6d805 102 int x = 0;
borlanic 0:fbdae7e6d805 103 float checksum = 0;
borlanic 0:fbdae7e6d805 104 for (int i=1; i < tokens.size()-1; i++) {
borlanic 0:fbdae7e6d805 105 newK[y][x] = strtof((tokens[i]).c_str(), 0);
borlanic 0:fbdae7e6d805 106 //pc.printf("Element %d , %d: %.10f\r\n", y, x, newK[y][x]);
borlanic 0:fbdae7e6d805 107 x++;
borlanic 0:fbdae7e6d805 108 checksum += newK[y][x];
borlanic 0:fbdae7e6d805 109 if (x == 15){
borlanic 0:fbdae7e6d805 110 x = 0;
borlanic 0:fbdae7e6d805 111 y++;
borlanic 0:fbdae7e6d805 112 }
borlanic 0:fbdae7e6d805 113 }
borlanic 0:fbdae7e6d805 114 }
borlanic 0:fbdae7e6d805 115 else{
borlanic 0:fbdae7e6d805 116 //pc.printf("Data to short od long\r\n");
borlanic 0:fbdae7e6d805 117 }
borlanic 0:fbdae7e6d805 118
borlanic 0:fbdae7e6d805 119 }
borlanic 0:fbdae7e6d805 120 else if (tokens[0] == "G") {
borlanic 0:fbdae7e6d805 121 //pc.printf("Send data to pc\r\n");
borlanic 0:fbdae7e6d805 122 if(sendData == false) {
borlanic 0:fbdae7e6d805 123 sendData = true;
borlanic 0:fbdae7e6d805 124 runCount = 0;
borlanic 0:fbdae7e6d805 125 }
borlanic 0:fbdae7e6d805 126 else{
borlanic 0:fbdae7e6d805 127 sendData = false;
borlanic 0:fbdae7e6d805 128 }
borlanic 0:fbdae7e6d805 129 }
borlanic 0:fbdae7e6d805 130
borlanic 0:fbdae7e6d805 131 else if (tokens[0] == "N") {
borlanic 0:fbdae7e6d805 132 //pc.printf("\r\nN\r\n");
borlanic 0:fbdae7e6d805 133 float data[4] = {};
borlanic 0:fbdae7e6d805 134 for (int i=1; i < tokens.size(); i++) {
borlanic 0:fbdae7e6d805 135 data[i-1] = strtof((tokens[i]).c_str(), 0);
borlanic 0:fbdae7e6d805 136 //pc.printf("Element %d %.2f\r\n", i,data[i-1]);
borlanic 0:fbdae7e6d805 137 }
borlanic 0:fbdae7e6d805 138 this->gainG = data[0];
borlanic 0:fbdae7e6d805 139 this->gain_dG = data[1];
borlanic 0:fbdae7e6d805 140 this->offsetX = data[2];
borlanic 0:fbdae7e6d805 141 this->offsetY = data[3];
borlanic 0:fbdae7e6d805 142 this->dataRecived = true;
borlanic 0:fbdae7e6d805 143 //pc1.printf("\r\ %d %.2f %.2f\r\n",dataRecived,gainG,gain_dG);
borlanic 0:fbdae7e6d805 144
borlanic 0:fbdae7e6d805 145 }
borlanic 0:fbdae7e6d805 146 else{
borlanic 0:fbdae7e6d805 147 //pc.printf("Command not understood\r\n");
borlanic 0:fbdae7e6d805 148 }
borlanic 0:fbdae7e6d805 149
borlanic 0:fbdae7e6d805 150
borlanic 0:fbdae7e6d805 151 tokens.clear();
borlanic 0:fbdae7e6d805 152 input.clear();
borlanic 0:fbdae7e6d805 153
borlanic 0:fbdae7e6d805 154
borlanic 0:fbdae7e6d805 155 }
borlanic 0:fbdae7e6d805 156 if(sendData == true) {
borlanic 0:fbdae7e6d805 157 if (output.size() == 0 && runCount >= 1000){
borlanic 0:fbdae7e6d805 158 output += "Hello nicolas\n";
borlanic 0:fbdae7e6d805 159 runCount = 0;
borlanic 0:fbdae7e6d805 160 }
borlanic 0:fbdae7e6d805 161 }
borlanic 0:fbdae7e6d805 162 runCount++;
borlanic 0:fbdae7e6d805 163 while (serial.writeable() && (output.size() > 0)) {
borlanic 0:fbdae7e6d805 164 serial.putc(output[0]);
borlanic 0:fbdae7e6d805 165 output.erase(0, 1);
borlanic 0:fbdae7e6d805 166 }
borlanic 0:fbdae7e6d805 167
borlanic 0:fbdae7e6d805 168 }
borlanic 0:fbdae7e6d805 169
borlanic 0:fbdae7e6d805 170 }
borlanic 0:fbdae7e6d805 171
borlanic 0:fbdae7e6d805 172
borlanic 0:fbdae7e6d805 173 void SerialCom::splitString() {
borlanic 0:fbdae7e6d805 174 stringstream ss(input);
borlanic 0:fbdae7e6d805 175 string item;
borlanic 0:fbdae7e6d805 176 while (getline(ss, item, DELIM)) {
borlanic 0:fbdae7e6d805 177 tokens.push_back(item);
borlanic 0:fbdae7e6d805 178 }
borlanic 0:fbdae7e6d805 179 return;
borlanic 0:fbdae7e6d805 180 }
borlanic 0:fbdae7e6d805 181
borlanic 0:fbdae7e6d805 182