Guest User

Untitled

a guest
Jun 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include "ballCoordinate.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <arduino.h>
  5.  
  6. char dataStream[DATALENGTH];
  7. uint8_t numOfPoint;
  8.  
  9. uint8_t* color;
  10. uint8_t* simpleCoordinate;
  11. int16_t* y;
  12. int16_t* z;
  13.  
  14. bool getDataStream(void){
  15. // code Reuse from last year competition
  16.  
  17. //first clear all data in the struct
  18. memset(dataStream, 0, DATALENGTH);
  19.  
  20. // Serial Communication with Raspberry Pi on Serial port 1 begins
  21. Serial.begin(115200);
  22.  
  23. Serial.println("GO");
  24.  
  25. while(Serial.available() == 0 ){}
  26.  
  27. int dataindex = 0;
  28. while(Serial.available()> 0){
  29.  
  30. // Fills datastream
  31. if(dataindex < DATALENGTH-1)
  32. {
  33. char inChar = Serial.read(); // Read a character
  34. dataStream[dataindex] = inChar; // Store it
  35. dataindex++; // Increment where to write next
  36. }
  37. delayMicroseconds(100);
  38. }
  39. dataStream[dataindex] = '\0'; // Null terminate the string
  40. Serial.flush();
  41.  
  42. Serial.print("DataStream(contain): ");Serial.println(dataStream);
  43. return true;
  44. }
  45.  
  46. bool parseData(void){
  47.  
  48. //split package to main token by ';'
  49. char **stringToken = NULL;
  50. stringToken = split(dataStream,';',&numOfPoint);
  51.  
  52. //store number of coordinate is passing
  53. numOfPoint = atoi(stringToken[0]);
  54.  
  55. // exit if receive bad data;
  56. //TODO: added more validation
  57. if(numOfPoint > 2 || numOfPoint < 1) {
  58. //sSerial.println("fail in parse");
  59. return false;
  60. }
  61.  
  62. //set dynamic array size.
  63. //create dynamic array size.
  64. // make sure it only have a size of 1 or 2 acordingly to numOfPoint.
  65. color = (uint8_t*) malloc(sizeof(uint8_t) * numOfPoint);
  66. y = (int16_t*) malloc(sizeof(int16_t) * numOfPoint);
  67. z = (int16_t*) malloc(sizeof(int16_t) * numOfPoint);
  68.  
  69. // transformation from pixel coordinate relative to the camera
  70. // To coordinate that respect to the Robot Arm
  71.  
  72. char **pointToken = NULL;
  73. for(int i = 0;i < numOfPoint; i++){
  74. uint8_t n = 0 ;// dummies varable for debug or output valiedation
  75. pointToken = split(stringToken[i+1],',',&n);
  76.  
  77. //deposit in to global variable
  78. color[i] = atoi(pointToken[0]);
  79. y[i] = atoi(pointToken[1]);
  80. z[i] = atoi(pointToken[2]);
  81. }
  82.  
  83.  
  84. // free all 2d array token to prevent mem leak
  85. for(int i = 0; i < numOfPoint; i++) free(pointToken[i]);
  86. free(pointToken);
  87. for(int i = 0; i < numOfPoint; i++) free(stringToken[i]);
  88. free(stringToken);
  89.  
  90. //TODO: ERROR checking and return true if success ful else return false;
  91. printPoint();
  92. return true;
  93. }
  94.  
  95. void printPoint(){
  96. for(int i = 0; i<numOfPoint;i++){
  97. Serial.print(color[i]);Serial.print(",");Serial.print(y[i]);Serial.print(",");Serial.print(z[i]);
  98. Serial.println("");
  99. }
  100. }
  101.  
  102. void transformation(double *xVal, double *yVal,const double zVal){
  103. // convert to real inches value from pixel
  104. }
  105.  
  106. void parseSimpleData(void){
  107. int *n = 0; //number of coordinate
  108. //split package to main token by ';'
  109. char **stringToken = NULL;
  110. stringToken = split(dataStream,';',&n);
  111.  
  112. //store number of coordinate is passing
  113. numOfPoint = atoi(stringToken[0]);
  114.  
  115. // exit if receive bad data;
  116. //TODO: added more validation
  117. if(numOfPoint > 2 || numOfPoint < 1) {
  118. //sSerial.println("fail in parse");
  119. return false;
  120. }
  121.  
  122. //set dynamic array size.
  123. //create dynamic array size.
  124. // make sure it only have a size of 1 or 2 acordingly to numOfPoint.
  125. color = (uint8_t*) malloc(sizeof(uint8_t) * numOfPoint);
  126. simpleCoordinate = (int8_t*) malloc(sizeof(int8_t) * numOfPoint);
  127.  
  128. char **pointToken = NULL;
  129. for(int i = 0;i < n; i++){
  130. uint8_t d = 0 ;// dummies varable for debug or output valiedation
  131. pointToken = split(stringToken[i+1],',',&d);
  132.  
  133. //deposit in to global variable
  134. color[i] = pointToken[0][0];
  135. simpleCoordinate[i] = atoi(pointToken[0][1]);
  136. }
  137.  
  138.  
  139. // free all 2d array token to prevent mem leak
  140. for(int i = 0; i < numOfPoint; i++) free(pointToken[i]);
  141. free(pointToken);
  142. for(int i = 0; i < numOfPoint; i++) free(stringToken[i]);
  143. free(stringToken);
  144.  
  145. return true;
  146. }
  147.  
  148. char ** split(const char *str, char delimiter, uint8_t *n) {
  149.  
  150. int i, numDelimiters = 0;
  151. char delimit[] = {delimiter, '\0'};
  152. char *s = deepCopy(str);
  153. for(i=0; i<strlen(str); i++) {
  154. if(str[i] == delimiter) {
  155. numDelimiters++;
  156. }
  157. }
  158.  
  159. char **result = (char **) malloc(sizeof(char**) * (numDelimiters+1));
  160. char *token = strtok(s, delimit);
  161. i = 0;
  162. while(token != NULL) {
  163. result[i] = deepCopy(token);
  164. token = strtok(NULL, delimit);
  165. i++;
  166. }
  167.  
  168. free(s);
  169. *n = (numDelimiters+1);
  170. return result;
  171. }
  172.  
  173. char * deepCopy(const char *s) {
  174. char *copy = (char *) malloc( (strlen(s) + 1) * sizeof(char) );
  175. strcpy(copy, s);
  176. return copy;
  177. }
Add Comment
Please, Sign In to add comment