Advertisement
microrobotics

Balboa 32U4 Balancing Robot Kit for Raspberry Pi

Apr 3rd, 2023
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. /*
  2. This code demonstrates how to use the Balboa 32U4 Balancing Robot Kit for Raspberry Pi to balance the robot using a combination of a compass sensor and a gyro sensor. The Balboa32U4, LSM303, and L3G libraries are used to control the motors and read sensor data.
  3.  
  4. To use this code, assemble the Balboa 32U4 Balancing Robot Kit according to the instructions provided in the product documentation, and connect it to your Raspberry Pi using the provided USB cable. The compass and gyro sensors should be connected to the I2C bus of the Balboa 32U4 board according to the wiring diagram provided in the product documentation. The code reads the sensor data using the read() function of the LSM303 and L3G libraries, and calculates the heading angle and angular velocity. It then adjusts the motor speeds using the setSpeeds() function of the Balboa32U4Motors library based on the orientation of the robot and the angular velocity. The LED pin is also toggled every second to indicate the program is running.
  5.  
  6. Note that balancing a robot is a complex task that requires careful tuning of the motor control algorithm and calibration of the sensors. The code provided here is a basic example that may need to be modified and optimized depending on the characteristics of your robot and the environment it operates in.
  7. */
  8.  
  9. #include <Wire.h>
  10. #include <LSM303.h>
  11. #include <L3G.h>
  12. #include <Balboa32U4.h>
  13.  
  14. Balboa32U4Motors motors;
  15. LSM303 compass;
  16. L3G gyro;
  17.  
  18. const int led_pin = 13; // Pin connected to the onboard LED
  19.  
  20. void setup() {
  21.   Serial.begin(9600); // Initialize the serial communication
  22.   motors.setSpeeds(0, 0); // Set both motors to 0 speed
  23.   pinMode(led_pin, OUTPUT); // Set the LED pin as an output
  24.   compass.init(); // Initialize the compass sensor
  25.   gyro.init(); // Initialize the gyro sensor
  26.   gyro.enableDefault(); // Enable the gyro sensor with default settings
  27. }
  28.  
  29. void loop() {
  30.   // Read the sensor data
  31.   compass.read();
  32.   gyro.read();
  33.  
  34.   // Calculate the heading angle
  35.   float heading = compass.heading();
  36.   heading = heading < 0 ? heading + 360 : heading; // Convert negative heading to positive
  37.  
  38.   // Calculate the angular velocity
  39.   float angular_velocity = gyro.g.z;
  40.  
  41.   // Print the sensor data to the serial monitor
  42.   Serial.print("Heading: ");
  43.   Serial.print(heading);
  44.   Serial.print(" degrees, Angular Velocity: ");
  45.   Serial.print(angular_velocity);
  46.   Serial.println(" dps");
  47.  
  48.   // Check the orientation of the robot and adjust the motor speeds accordingly
  49.   if (heading >= 45 && heading <= 315) {
  50.     // Robot is tilted forwards or backwards, adjust motor speeds to balance
  51.     int speed = constrain(map(angular_velocity, -1000, 1000, -400, 400), -400, 400);
  52.     motors.setSpeeds(speed, speed);
  53.   } else {
  54.     // Robot is upright, stop the motors
  55.     motors.setSpeeds(0, 0);
  56.   }
  57.  
  58.   // Toggle the LED every second
  59.   digitalWrite(led_pin, millis() % 1000 < 500 ? HIGH : LOW);
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement