Instructables

Make:it Robotics Starter Kit - Sending Sensor Data Wirelessly

Picture of Make:it Robotics Starter Kit - Sending Sensor Data Wirelessly

In this Instructable we are going to take what we learned in “Make:it Robotics Starter Kit – Wireless Connectivity” and “Make:it Robotics Starter Kit – Capturing Sensor Data” and combine this data to capture sensor data in real time and send this data to a program running on our computer wirelessly.

So if you do not have your robot configured to use the Transmitter and receiver, take a look at the blog post “Make:it Robotics Starter Kit – Capturing Sensor Data” and re-configure your robot and FTDI cable.

In order to send sensor data to our computer we again have to make a few small changes to our lineFollow.ino Arduino program:

Comment out the following line, as we no longer need our counter variable.

// int counter;

Comment out the variable initialization of the counter variable:

// counter = 0;

In the loop() function of the program comment out the following lines:

// mySerial.println(counter, DEC);

// counter++;

Add the following line to the loop() function:

mySerial.println(action1, HEX);

if(action1 != action2)

{

if (action1 == 3 )

line_following.go_forward(50);

if (action1 == 1)

line_following.line_following_turn_left(50);

if (action1 == 2)

line_following.line_following_turn_right(50);

if (action1 == 0)

line_following.go_forward(50);

}

action2=action1;

}

When we run the robot our sensor data will be send out wirelessly to our computer, but we do not want to just capture the serial data using the Serial Monitor with the results being displayed as hex numbers.


We want to display in real time the actual directions the robot is being given based on the sensor data. In order to do this we cannot use the Serial Monitor.

We must write a program that has similar logic as our lineFollow.ino program. So we are going to develop a Python program that reads the serial port data and print out the directions that the Robot is being given in plain English.

Attached is the Python program, we will analyze the program in a bit. If you have not done so, refer to the earlier blog post “Make:it Robotics Starter Kit – Binary Part 1″ and watch the Video on how to setup Python on your computer.

import serial

import time


The import statements provide library functions that our Python program need in order to communicate to the serial port.

def main():

The statement defines a function called main()

ser = serial.Serial('COM3', 1200, timeout=0)

This statement creates a serial object and tells the object what serial port to listen on, what baud rate to use and timeout=0 is used to read the data when it is available without waiting.

while 1:

The while 1: statement is used to start a loop that repeats and does not end as the condition of 1 is always true.

value = ser.read(1)

Here we read one byte of data from the serial port and store this value to the value variable.

if value >= '0' and value <= '3':

If the contents of the value variable is between 0 and 3 execute the contents of the if statement

if value == '0' or value == '3'

print "Go Forward"

If the value variable is either 1 or 3 then execute the print “Go Foward”message.

if value == '1':

print "Turn Left"

If the value variable is 1 then print the message “Turn Left”

if value == '2':

print "Turn Right"

If the value variable is 2 then print the message “Turn Right”

if __name__ == "__main__":main()

If this program is being executed and not a library file then execute the main function.

Remember Python is an indent sensitive sensitive language. If your indentation is not correct your program will generate syntax errors when you try to execute it. Count your indentation spaces when coping the code in your text editor.
Remember when you plug your USB cable into to program the Arduino you will have to make sure your serial port is correct for the Serial Port Settings in the Arduino IDE.

Refer to the blog post “Make:it Robotics Starter Kit- Capturing Sensor Data” for information on how to determine the correct Serial Port and how to setup the Arduino IDE.

If you use a different USB Port for your FTDI USB cable, your Serial Port will be different as well.

Place your readSerial.py program in a folder on your computer and start a Command Prompt and change directory to the same folder location of your readSerial.py program.

Run the command from your Command Prompt:

Python runSerial.py

If you encounter any errors, check that your runSerial.py program is set to the correct serial port.

Make sure you have your driver board and FTDI cable wired correctly.

If you are getting strange characters being printed to your command prompt, check to make sure your baud rate is the same in the Arduino lineFollow.ino and readSerial.py programs.

If you did everything correct, when your robot is following the black line your Python program should output something shown in the image above.

readSerial.py679 bytes
 
Remove these adsRemove these ads by Signing Up
seamster4 months ago

Interesting. Thanks for sharing.

jpitz31 (author)  seamster4 months ago
Thanks for the comment.

Joe