1
\$\begingroup\$

I am building a robotic car with stepper motors (for wheels) and infrared sensors(avoiding obstracles) using PIC18F4550 microcontroller.

So, I have created a function to drive the stepper motor steps by steps and it works fine.

void move_forward(int steps) { // Code to drive stepper motor step by step } 

Then I created a function to read the infrared sensor output.

Let's say

int read_distance_from_sensor(void) { // Code to read the analog input and return distance } 

It works fine too. But my problem is how do I run the read_distance_from_sensor() function while the stepper motor function is running?

From what I understand, everything gets executed one after another in the PIC18 and it is not possible to execute 2 tasks at the same time simultaneously.

If I do the following:

void main(void) { while(1) { move_forward(20); if (read_distance_from_sensor<10) { // Stop } } } 

It will move forward first then it stop to get the distance measurement.

I want it to return the distance measurement while it is on the move.

Do I have to call the read_distance_from_sensor() function inside the move_forward() function everytime i move a step for motor?

Is there a clever way to accomplish this?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ You'd need threads, but this may be overkill. See stackoverflow.com/questions/1624237/… \$\endgroup\$ Commented May 4, 2021 at 10:19
  • 1
    \$\begingroup\$ Rather than moving forward 20, why not move forward 1, read sensor, move forward 1 ... \$\endgroup\$ Commented May 4, 2021 at 10:19
  • 2
    \$\begingroup\$ There are many options, including the use of threads (if you use some form of RTOS), timer-based interrupts, interleaving your code, using state machines... which one is the best option for you depends a lot on what you do in those functions, what other tasks you may have to perform at the same time, the time-sensitivity of each operation, the relationship between those operations, and more... We lack details. \$\endgroup\$ Commented May 4, 2021 at 10:50
  • 4
    \$\begingroup\$ Ignore everyone rambling about threads and RTOS... Multi-tasking on low-end microcontrollers is achieved by telling different hardware to do different things at the same time. If you for example drive a motor with PWM at the same time as you pick up readings with an ADC, then both the PWM and the ADC hardware peripherals can do their work without involving the CPU at all. The CPU just has to step in on regular basis and synchronize the PWM based on the ADC. So the answer to your question depends on how do you drive the stepper motor and how do you collect data from what kind of sensor? \$\endgroup\$ Commented May 4, 2021 at 12:53
  • 1
    \$\begingroup\$ Use the peripherals, use interrupts as required, and don't write blocking code. \$\endgroup\$ Commented May 4, 2021 at 20:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.