DEV Community

Cover image for Self-Aligning Dish in Rust: Command Application
Ian Ndeda
Ian Ndeda

Posted on

Self-Aligning Dish in Rust: Command Application

This part of the series will be an application of the preceding part, which enabled our project to receive commands remotely via Bluetooth.

Table of Contents

Requirements

  • 1 x Raspberry Pi Pico board
  • 1 x USB Cable type 2.0
  • 1 x HC-05 Bluetooth module
  • 10 x M-M jumper wires
  • 2 x Mini Breadboards
  • Serial Bluetooth Terminal App

Implementation

command-loop

In the main part of the program, under the super loop, we need to check whether we are in auto or manual mode. We'll also need to enter a critical section since shared variables will be used. 

Add handles for uart_cmd and direction.

cortex_m::interrupt::free(|cs| {
    let mut direction = DIRECTION.borrow(cs).borrow_mut();
    let mut uart_cmd = UARTCMD.borrow(cs).borrow_mut();

    if AUTO.borrow(cs).get() {
        // Auto mode
    } else {
        // Manual mode
    }
});
Enter fullscreen mode Exit fullscreen mode

Then add the following under the auto and manual arms respectively:

writeln!(&mut serialbuf, "auto mode").unwrap();
transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);

writeln!(&mut serialbuf, "manual mode").unwrap();
transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
Enter fullscreen mode Exit fullscreen mode

Under manual mode, we'll add the below code that applies our algorithm.

// manual mode
match direction.as_mut() {
    Some(Direction::Cw) => {
        *direction = None;// reset direction 
        writeln!(&mut serialbuf, "manual mode: cw").unwrap();
        transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
    },
    Some(Direction::Ccw) => {
        *direction = None; 
        writeln!(&mut serialbuf, "manual mode: ccw").unwrap();
        transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
    },
    Some(Direction::Up) => {
        *direction = None; 
        writeln!(&mut serialbuf, "manual mode: up").unwrap();
        transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
    },
    Some(Direction::Down) => {
        *direction = None; 
        writeln!(&mut serialbuf, "manual mode: down").unwrap();
        transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
    },
    Some(Direction::Zero) => {
        *direction = None; 
        writeln!(&mut serialbuf, "manual mode: position zero").unwrap();
        transmit_uart_cmd(uart_cmd.as_mut().unwrap(), serialbuf);
    },
    None => {},
}
Enter fullscreen mode Exit fullscreen mode

Connections

The connections will remain the same as from the previous part of the series.

command-connections

Results

After rearranging our code we will get this final copy.

Here are highlights of changes from the previous program.

Running this program will flash it into the Pico, and we'll be set to receive commands remotely via Bluetooth.

We can see that while in auto mode any further received commands other than A are not consequential.

command-results-1

In manual mode the system responds appropriately to further commands.

command-results-2

Next we will set up our system to receive GPS data from the Neo-6m GPS module.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay