Basebot main code

From Rsewiki
Revision as of 11:16, 4 September 2024 by Jca (talk | contribs) (Created page with "Back to [Basebot]] === Main code === The main code is in ''basebot_6.ino'' (sometimes renamed to ''basebot_6.cpp). The code is based on the Arduino environment. Here the normal C-main function is hidden. But ''setup()'' is called first, followed by an endless loop that calles ''loop()''. ===== Setup ===== The ''setup()'' function is like this and initializes all support modules: void setup() // INITIALIZATION { // to be able to print to USB interface Serial....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Back to [Basebot]]

Main code

The main code is in basebot_6.ino (sometimes renamed to basebot_6.cpp).

The code is based on the Arduino environment. Here the normal C-main function is hidden. But setup() is called first, followed by an endless loop that calles loop().

Setup

The setup() function is like this and initializes all support modules:

void setup()   // INITIALIZATION
{ // to be able to print to USB interface
  Serial.begin(12000000);
  // initialize sensors, safety and display
  robot.setup(); // alive LED, display and battery
  motor.setup(); // motor voltage
  encoder.setup(); // motor encoders to velocity
  imu2.setup(); // gyro and accelerometer - include tilt angle
}
Loop

The loop() function:

void loop ( void )
{ // init sample time
 uint32_t nextSample = 0;
 while ( true )
 { // main loop
   // get time since start in microseconds
   uint32_t us = micros();
   // loop until time for next sample
   if (us > nextSample) // start of new control cycle
   { // advance time for next sample
     nextSample += sampleTimeUs;
     // read sensors
     imu2.tick();
     encoder.tick();
     // advance test sequence (default is wait for start button)
     testSequence();
     // make control actions
     controlUpdate();
     // give value to actuators
     motor.tick();
     // support functions
     robot.tick(); // measure battery voltage etc.
     display.tick(); // update O-LED display
   }
   usb.tick(); // listen to incoming from USB
 }
}

The loop (while (true)) runs as fast as it can, and if it is not time for a new sample, then the USB connection is serviced (usb.tick()).

When the sample time has passed, the next sample time is set up. Both use the standard Arduino call micros(), which returns the number of microseconds since boot.

At every sample, the sensors are handled first (imu.tick() and encoder.tick()); these will get new data from the sensor, and the encoder will estimate the motor velocity.

Next is testSequenct(), which is intended as a state machine that changes what the robot should do, as described below. The default is to wait for the start button to be pressed.

All data for the control effort is now available (controlUpdate()). The default controller uses the desired velocity as motor voltage, ignoring the estimated velocity. This is a feed-forward-only controller.

The control effort sets the motor voltages and the motor.tick() implements this as a PWM to the motor driver.

In the end, other service functions are called (robot.tick() and display.tick()), which (mostly) check the battery voltage and update the display.

State update