Guide on Using your Robot

Jump to:


Pico's LED

Pico's Onboard LED

How to use:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Setup for wifi chip (cyw43)
    2. Turns on Pico's LED
    3. Within while() loop, blinks led

                #include "rcc_stdlib.h"
                using namespace std;
    
                int main()
                {
                    stdio_init_all();    
                    sleep_ms(1500);
                    cyw43_arch_init(); //setup 

                    cyw43_arch_gpio_put(0,1); //turns on LED

                    cout << "gonna blink now;)" << '\n';

                    while(true)
                    {   
                        sleep_ms(300);
                        cyw43_arch_gpio_put(0, !cyw43_arch_gpio_get(0)); //blinks LED
                    }
                }
            

Raft-Input Sensors

Push Button

Our button:

How it works:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up button
    3. In while()loop:
      1. Checks if button is pressed

            #include "rcc_stdlib.h"
            using namespace std;

            int main()
            {
                stdio_init_all();    
                sleep_ms(1500);
                cyw43_arch_init(); //setup 
                cyw43_arch_gpio_put(0,1); //turns on LED

                rcc_init_pushbutton(); //set up button

                while(true)
                {   
                        if(!gpio_get(RCC_PUSHBUTTON)) //if gpio NOT high
                        {
                            //do stuff here when button pressed
                            cout << "PUSHBUTTON PRESSED!" << '\n'; 
                        }
                }
            }
            

Potentiometer

Our potentiometer:

How it works:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up potentiometer's ADC
    3. In while()loop:
      1. converts voltage to a digital reading using adc_read()

            #include "rcc_stdlib.h"
            using namespace std;
            
            int main()
            {
                stdio_init_all();
                sleep_ms(1500);
                cyw43_arch_init(); //setup 
                cyw43_arch_gpio_put(0,1); //led on
            
                rcc_init_potentiometer(); //setup ADC
            
                while(true)
                {
                        int32_t pot_val = adc_read(); //values from 0 to +4097
                        cout << "raw value: " << pot_val << '\n';
                        sleep_ms(100);
                }
            }
            

Actuators

DC Motors

How a motor works:

Our Motor Controller:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up motor struct
    3. Sets left and right to ENA and ENB
    4. Enables PWM
    5. In while()loop:
      1. Sets motor power

                #include "rcc_stdlib.h"
                using namespace std;
                
                int main()
                {
                    stdio_init_all();
                    sleep_ms(1500);
                    cyw43_arch_init(); //setup 
                    cyw43_arch_gpio_put(0,1); //led on
                
                    Motor motors; //struct setup
                    MotorInit(&motors, RCC_ENA, RCC_ENB, 1000); //setup left and right
                    MotorsOn(&motors); //enable PWM
                
                    while(true)
                    {
                        MotorPower(&motors, 100, 100);  // %motor speed
                        cout << "motors going forwards" << '\n';
                        sleep_ms(100);
                    }
                }
            

Servo Motor

How a servo motor works:

Our Servo Motor:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up servo struct
    3. Sets to GPIO 18
    4. Enables PWM
    5. In while()loop:
      1. Sets servo position

                #include "rcc_stdlib.h"
                using namespace std;
                
                int main()
                {
                    stdio_init_all();
                    sleep_ms(1500);
                    cyw43_arch_init(); //setup 
                    cyw43_arch_gpio_put(0,1); //led on
                
                    Servo s3; //struct setup
                    ServoInit(&s3, 18, false, 50); //setup 
                    ServoOn(&s3); //enable PWM
                
                    while(true)
                    {
                        ServoPosition(&s3, 50); //50% of range (forwards)
                        cout << "servo facing forwards" << '\n';
                        sleep_ms(100);
                    }
                }
            

Sensors

Lidar Sensor

How the Lidar Works:

Our Lidar Sensor:

How it communicates:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up i2c on the Pico
    3. Lidar's class setup
    4. Sets to i2c1
    5. In while()loop:
      1. returns a distance in millimeters
      2. a reading of >8000 means objects are too far away to detect

                    #include "rcc_stdlib.h"
                    using namespace std;
                    
                    int main()
                    {
                        stdio_init_all();
                        sleep_ms(1500);
                        cyw43_arch_init(); //setup 
                        cyw43_arch_gpio_put(0,1); //led on
                    
                        rcc_init_i2c(); //setup pico i2c

                        VL53L0X lidar; //class
                        rcc_init_lidar(&lidar); //setup lidar on i2c1
                    
                        while(true)
                        {
                            uint16_t dist = getFastReading(&lidar);
                            cout << "distance: " << dist << "\n";
                            sleep_ms(100);
                        }
                    }
                

IMU

Inertial Measurement Unit (IMU)

How it works:

Our IMU:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Setup i2c
    3. Sets up imu class
    4. Begins communication with imu
    5. Calibrates sensor
    6. In while()loop:
      1. updates pico on all the data
      2. gets angular velocity around z-axis

                #include "rcc_stdlib.h"
                using namespace std;
                
                int main()
                {
                    stdio_init_all();
                    sleep_ms(1500);
                    cyw43_arch_init(); //setup 
                    cyw43_arch_gpio_put(0,1); //led on
                
                    rcc_init_i2c(); //setup i2c

                    MPU6050 imu; //class
                    imu.begin(i2c1); //adds to i2c1
                    imu.calibrate(); //hold robot still
                
                    while(true)
                    {
                        imu.update_pico(); //updates data
                        float angvel_z = imu.getAngVelZ(); //call function to get data
                        cout <<" z rotation: " << angvel_z << "\n";
                        sleep_ms(100);
                    }
                }
            

Encoders

Optical Interrupt Sensors

Using Interrupts:

Two functions to know:

Example Code:

  1. Includes RCC library
  2. Within main()function:
    1. Turns on Pico's LED
    2. Sets up classes for left and right
    3. In while()loop:
      1. Gets count for both wheels
      2. If left count is over 100, resets left count
      3. If right count is over 200, resets right count

                #include "rcc_stdlib.h"
                using namespace std;
                
                int main()
                {
                    stdio_init_all();
                    sleep_ms(1500);
                    cyw43_arch_init(); //setup 
                    cyw43_arch_gpio_put(0,1); //led on
                
                    Left_Odom left; //class
                    Right_Odom right; //class

                    while(true)
                    {
                        int left_count = left.getCount(); //current left count
                        int right_count = right.getCount(); //current right count
                
                        if (left_count>=100){
                            left.setZero(); //resets left count
                        }	
                        if (right_count>=200){
                            right.setZero(); //resets right count
                        }
                
                        cout << left_count << " | " << right_count << '\n';
                        sleep_ms(100);
                    }
                }