suraj0152

t

May 8th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sra_board.h"
  4.  
  5. void left_shift(){
  6.     //implement a logic to blink LEDs in a pattern from right to left.
  7. }
  8.  
  9. void blink_task(void *pvParameters) {
  10.    switch(node_type) {
  11.         case 1: // ONLY LEFT || LEFT+STRAIGHT
  12.             left_shift();
  13.             break;
  14.         case 2: // ONLY RIGHT
  15.             right_sift();
  16.             break;
  17.         case 3: // RIGHT+STRAIGHT
  18.             towards_center();
  19.             break;
  20.         case 4: // DEAD END
  21.             stationary();
  22.             break;
  23.         case 5: // PLUS
  24.             blink();
  25.             break;
  26.     }
  27. }
  28.  
  29. void motor_task(void *pvParameters) {
  30.     uint8_t turn_type = *(uint8_t *)pvParameters;
  31.     switch(turn_type) {
  32.         case 1: // Left turn
  33.             set_motor_speed(motor_a_0, MOTOR_BACKWARD, motor_pwm);
  34.             set_motor_speed(motor_a_1, MOTOR_FORWARD, motor_pwm);
  35.             vTaskDelay(1000 / portTICK_PERIOD_MS);
  36.             break;
  37.         case 2: // Right turn
  38.             set_motor_speed(motor_a_0, MOTOR_FORWARD, motor_pwm);
  39.             set_motor_speed(motor_a_1, MOTOR_BACKWARD, motor_pwm);
  40.             vTaskDelay(1000 / portTICK_PERIOD_MS);
  41.             break;
  42.         case 3: // Straight
  43.             //set_motor_speed------
  44.             break;
  45.         case 4: // U-turn
  46.             //set_motor_speed------
  47.             break;
  48.     }
  49.     vTaskDelete(NULL);
  50. }
  51.  
  52. void process_node_turn(uint8_t node_type, uint8_t turn_type) {
  53.     // Create tasks for LED blinking and motor control movement
  54.     xTaskCreate(blink_task, "blink_task", 4096, &node_type, 1, NULL);
  55.     xTaskCreate(motor_task, "motor_task", 4096, &turn_type, 1, NULL);
  56. }
  57.  
  58. void read_and_process_txt_file(const char *filename) {
  59.     FILE *file = fopen(filename, "r");
  60.     if (file == NULL) {
  61.         printf("Error opening file.\n");
  62.         return;
  63.     }
  64.  
  65.     char line[100];
  66.     while (fgets(line, sizeof(line), file)) {
  67.         char node[20], turn[20], direction[20];
  68.         sscanf(line, "%s %s %s", node, turn, direction);
  69.  
  70.         uint8_t node_type, turn_type;
  71.  
  72.         // Assign node type based on the node description
  73.         if (strcmp(node, "ONLY LEFT") == 0 || strcmp(node, "LEFT+STRAIGHT") == 0)
  74.             node_type = 1; // Node type: ONLY LEFT or LEFT+STRAIGHT
  75.         else if (strcmp(node, "ONLY RIGHT") == 0)
  76.             node_type = 2; // Node type: ONLY RIGHT
  77.         else if (strcmp(node, "RIGHT+STRAIGHT") == 0)
  78.             node_type = 3; // Node type: RIGHT+STRAIGHT
  79.         else if (strcmp(node, "PLUS") == 0)
  80.             node_type = 4; // Node type: PLUS
  81.         else if (strcmp(node, "DEAD END") == 0)
  82.             node_type = 5; // Node type: DEAD END
  83.         else
  84.             node_type = 0; // Invalid node
  85.  
  86.         // Assign turn type based on the turn description
  87.         if (strcmp(turn, "LEFT") == 0)
  88.             turn_type = 1; // Turn type: LEFT
  89.         else if (strcmp(turn, "RIGHT") == 0)
  90.             turn_type = 2; // Turn type: RIGHT
  91.         else if (strcmp(turn, "STRAIGHT") == 0)
  92.             turn_type = 3; // Turn type: STRAIGHT
  93.         else if (strcmp(turn, "UTURN") == 0)
  94.             turn_type = 4; // Turn type: UTURN
  95.         else
  96.             turn_type = 0; // Invalid turn
  97.  
  98.         process_node_turn(node_type, turn_type);
  99.     }
  100.  
  101.     fclose(file);
  102. }
  103.  
  104. void app_main() {
  105.  
  106.     char filename[100];
  107.     printf("Enter the path to the .txt file: ");
  108.     scanf("%s", filename);
  109.  
  110.     // Read and process the .txt file
  111.     read_and_process_txt_file(filename);
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment