Advertisement
pleasedontcode

"Sensor Control" rev_16

Jun 23rd, 2024
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Sensor Control"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-24 04:58:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* take input from 4 hall sensor for each wheel */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* take input pwm signal and send out to 4 brushless */
  23.     /* esc for each wheel. change the motor speed to */
  24.     /* maintain traction */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Arduino.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void readHallSensors(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t hallSensor1_PIN_A0 = A0;
  39. const uint8_t hallSensor2_PIN_A1 = A1;
  40. const uint8_t hallSensor3_PIN_A2 = A2;
  41. const uint8_t hallSensor4_PIN_A3 = A3;
  42. const uint8_t pwm_PIN_D2 = 2;
  43.  
  44. /***** DEFINITION OF PWM OUTPUT PINS *****/
  45. const uint8_t output1_PIN_D3 = 3;
  46. const uint8_t output2_PIN_D4 = 4;
  47. const uint8_t output3_PIN_D5 = 5;
  48. const uint8_t output4_PIN_D6 = 6;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. uint8_t output1_PIN_D3_rawData = 0;
  53. uint8_t output2_PIN_D4_rawData = 0;
  54. uint8_t output3_PIN_D5_rawData = 0;
  55. uint8_t output4_PIN_D6_rawData = 0;
  56.  
  57. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  58. /***** used to store data after characteristic curve transformation *****/
  59. float output1_PIN_D3_phyData = 0.0;
  60. float output2_PIN_D4_phyData = 0.0;
  61. float output3_PIN_D5_phyData = 0.0;
  62. float output4_PIN_D6_phyData = 0.0;
  63.  
  64. /***** DEFINITION OF HALL SENSOR VARIABLES *****/
  65. /***** used to store hall sensor data *****/
  66. int hallSensor1_value = 0;
  67. int hallSensor2_value = 0;
  68. int hallSensor3_value = 0;
  69. int hallSensor4_value = 0;
  70.  
  71. void setup(void)
  72. {
  73.     // put your setup code here, to run once:
  74.     pinMode(hallSensor1_PIN_A0, INPUT);
  75.     pinMode(hallSensor2_PIN_A1, INPUT);
  76.     pinMode(hallSensor3_PIN_A2, INPUT);
  77.     pinMode(hallSensor4_PIN_A3, INPUT);
  78.     pinMode(pwm_PIN_D2, INPUT);
  79.  
  80.     pinMode(output1_PIN_D3, OUTPUT);
  81.     pinMode(output2_PIN_D4, OUTPUT);
  82.     pinMode(output3_PIN_D5, OUTPUT);
  83.     pinMode(output4_PIN_D6, OUTPUT);
  84. }
  85.  
  86. void loop(void)
  87. {
  88.     // put your main code here, to run repeatedly:
  89.     readHallSensors(); // Read hall sensor data
  90.     updateOutputs(); // Refresh output data
  91. }
  92.  
  93. void readHallSensors()
  94. {
  95.     // Read the value from each hall sensor
  96.     hallSensor1_value = analogRead(hallSensor1_PIN_A0);
  97.     hallSensor2_value = analogRead(hallSensor2_PIN_A1);
  98.     hallSensor3_value = analogRead(hallSensor3_PIN_A2);
  99.     hallSensor4_value = analogRead(hallSensor4_PIN_A3);
  100.  
  101.     // Process the hall sensor data to control motor speed
  102.     // This is a placeholder for the actual logic to maintain traction
  103.     output1_PIN_D3_rawData = map(hallSensor1_value, 0, 1023, 0, 255);
  104.     output2_PIN_D4_rawData = map(hallSensor2_value, 0, 1023, 0, 255);
  105.     output3_PIN_D5_rawData = map(hallSensor3_value, 0, 1023, 0, 255);
  106.     output4_PIN_D6_rawData = map(hallSensor4_value, 0, 1023, 0, 255);
  107. }
  108.  
  109. void updateOutputs()
  110. {
  111.     analogWrite(output1_PIN_D3, output1_PIN_D3_rawData);
  112.     analogWrite(output2_PIN_D4, output2_PIN_D4_rawData);
  113.     analogWrite(output3_PIN_D5, output3_PIN_D5_rawData);
  114.     analogWrite(output4_PIN_D6, output4_PIN_D6_rawData);
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement