Giter Club home page Giter Club logo

autonomous-rc-car's Introduction

autonomous-rc-car

Gitter

This project aims to build an autonomous rc car using supervised learning of a neural network with a single hidden layer. We have not used any Machine Learning libraries since we wanted to implement the neural network from scratch to understand the concepts better. We have modified a remote controlled car to remove the dependency on the RF remote controller. A Raspberry Pi controls the DC motors via an L293D Motor Driver IC. You can find a post explaining this project in detail here. Here's a video of the car in action.

Autonomous RC car

Configuration

Rc car controller circuit diagram

We will be referring the DC motor controlling the left/right direction as the front motor and the motor controlling the forward/reverse direction as the back motor. Connect the BACK_MOTOR_DATA_ONE and BACK_MOTOR_DATA_TWO GPIO pins(GPIO17 and GPIO27) of the Raspberry Pi to the Input pins for Motor 1(Input 1, Input 2) and the BACK_MOTOR_ENABLE_PIN GPIO pin(GPIO22) to the Enable pin for Motor 1(Enable 1,2) in the L293D Motor Driver IC. Connect the Output pins for Motor 1(Output 1, Output 2) of the IC to the back motor.

Connect the FRONT_MOTOR_DATA_ONE and FRONT_MOTOR_DATA_TWO GPIO pins(GPIO19 and GPIO26) of the Raspberry Pi to the Input pins for Motor 2(Input 3, Input 4) in the IC. Connect the Output pins for Motor 2(Output 3, Output 4) of the IC to the front motor.

The PWM_FREQUENCY and INITIAL_PWM_DUTY_CYCLE represent the initial frequency and duty cycle of the PWM output.

We have created five class labels namely forward, reverse, left, right and idle and assigned their expected values. All class labels would require a folder of the same name to be present in the current directory.

The input images resize to the dimension of the IMAGE_DIMENSION tuple value during training. The LAMBDA and HIDDEN_LAYER_SIZE values represent the default lambda value and the number of nodes in the hidden layer while training the neural network.

All these values are configurable in configuration.py.

Setup

The images for training are captured using interactive_control_train.py, the car is controlled using the direction arrows and all the images are recorded in the same folder along with the corresponding key press. At the command prompt, run the following command:

python interactive_control_train.py

Data cleaning is done before segregating the images into their respective class folders based on the key press indicated in their filenames.

Train

After segregating the images into their corresponding class folders, the neural network is trained using train.py which takes two optional arguments - lambda and hidden layer size; default values would be those specified in the configuration file. At the command prompt, run the following command:

python train.py 0.1 100

The images are loaded from the corresponding class folders and are assigned the class values indicated in the configuration file. The generated model is stored in the optimized_thetas folder as a pickle file. Here's a sample dataset and trained model to get you started.

Run

Once we have the trained model, the RC car is run autonomously using autonomous.py which takes an optional argument for the trained model; default will use the latest model in the optimized_thetas folder. At the command prompt, run the following command:

python autonomous.py

We are reducing the speed of the car when the model predicts a turn. The reduction is not being made by the neural network, though we hope to add a speed component in the future.

Planned features

We intend to add the following capabilities in the future:

  • Control speed of the car using neural networks
  • Stop signal detection
  • Obstacle detection

Special Thanks

Thanks to Andrew Ng for his Coursera course on Machine Learning.

About

Multunus logo

autonomous-rc-car is maintained and funded by Multunus Software Pvt. Ltd. The names and logos for Multunus are trademarks of Multunus Software Pvt. Ltd.

We built this to explore and learn Machine Learning concepts in our 20% investment time. We will be supporting this project during our investment time.

autonomous-rc-car's People

Contributors

vignesh1115 avatar vimalvnair avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

autonomous-rc-car's Issues

Optimized_thetas Issue

Why getting this error?
FileNotFoundError: No such File or Directory: Optimized_thetas/model_2017-06-3_21-08-44_10.0_h50.pkl

Image for training and prediction have different size?

in predict.py, image stream is cropped before prediction.

def _convert_stream_to_array(stream):
    ...
    image = image.crop((0, PICAMERA_RESOLUTION_HEIGHT / 2, PICAMERA_RESOLUTION_WIDTH, PICAMERA_RESOLUTION_HEIGHT))
    ...

but in train.py, it not do the same step in load_images_to_array, is this a bug?

Trainingdata

hi, i'm learning this project,and i want to get the training data in this program, because it's too waste time to collect training data with manually operate the car, so do you mind to give your training data or upload the training data?

Some Questions in Autonomous.py file

Hi @vaidy @manishrc i am little confused because i see some lines of codes cycle_count... what is the purpose adding this lines in each directions operations!

kindly 👍
while True:
stream = io.BytesIO()
camera.capture(stream, format='jpeg', use_video_port=True)
direction = predictor.predict(stream)
image_helper.save_image_with_direction(stream, direction)
stream.flush()
if direction == 'forward':
should_brake = True
left_cycle_count = right_cycle_count = 0
forward_cycle_count = reduce_speed(pwm, forward_cycle_count)
motor_driver_helper.set_front_motor_to_idle()
motor_driver_helper.set_forward_mode()
elif direction == 'left':
should_brake = True
forward_cycle_count = right_cycle_count = 0
left_cycle_count = increase_speed_on_turn(pwm, left_cycle_count)
motor_driver_helper.set_left_mode()
motor_driver_helper.set_forward_mode()
elif direction == 'right':
should_brake = True
forward_cycle_count = left_cycle_count = 0
right_cycle_count = increase_speed_on_turn(pwm, right_cycle_count)
motor_driver_helper.set_right_mode()
motor_driver_helper.set_forward_mode()
elif direction == 'reverse':
should_brake = True
motor_driver_helper.set_front_motor_to_idle()
motor_driver_helper.set_reverse_mode()
else:
if should_brake:
print("braking...")
motor_driver_helper.set_reverse_mode()
time.sleep(0.2)
should_brake = False
motor_driver_helper.set_idle_mode()
forward_cycle_count = left_cycle_count = right_cycle_count = 0
motor_driver_helper.change_pwm_duty_cycle(pwm, 100)
print(direction)

PyGame is not Working

Why PyGame is not working in my Raspberry PI!?
` while True:
for event in pygame.event.get():
if event.type in {pygame.K_q, pygame.K_ESCAPE}:
print "Bye"
elif event.type in {pygame.KEYDOWN, pygame.KEYUP}:
print "Key pressed"
if event.type == pygame.KEYUP:

                print "UP"`

This is my Sample Code ! Actually it shows in terminal just random texts rather than UP
Thanks in Advance

Getting error in Train.py

Hello,Sir i'm getting error when i run Train.py.
FileNotFoundError:No such File or Directory: Optimized_thetas/model_2017-05-29_21-08-44_10.0_h50.pkl

camera no video

when python interactive_control_train.py,there's no video from camera ,but images can generate into the folder ,what's wrong?

autonomous.py issue

while running the autonomous.py script it gives an error " UnicodeDecodeError: 'ascii' codec cant decode byte 0x89 in position 9: ordinal not in range(128)?

Thanks in advance.

I'm getting runtime error overflow for power

hello sir I'm getting runtime error return 1.0/(1.0 + power(e, -x_value))) overflow with power. I have changed different lamda values but of no use and also i have decreased the size of the resolution.
Can you please help me sir....!!

wheel constantly moving backward

whenever I run interactive_control_train.py wheels start moving in backward direction. all other controls works fine but wheels keep moving backward and stops when i press forward button and again start moving when i release forward button

train.py compilation error

Im having an issue when execute the command of "train.py".I have done every procedure accordingly . The error is given below . Please Help ASAP
python version used : python 2.7

pi@AutoCar:~/autonomous-rc-car-master $ python train.py 0.1 60 Loading images to array... Traceback (most recent call last): File "train.py", line 114, in <module> main() File "train.py", line 107, in main (optimized_theta, function_min_value, info_dict) = minimize_cost_function(initial_theta,function_parameters) File "train.py", line 72, in minimize_cost_function fprime=gradients_wrapper, args=[function_parameters]) File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 186, in fmin_l_bfgs_b **opts) File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 314, in _minimize_lbfgsb f, g = func_and_grad(x) File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 265, in func_and_grad f = fun(x, *args) File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 281, in function_wrapper return function(*(wrapper_args + args)) File "train.py", line 32, in cost_function_wrapper return cost_function(cost_function_parameters) File "/home/pi/autonomous-rc-car-master/cost_function.py", line 35, in cost_function regularization_term = (lambda_value/(2.0 * input_examples_size)) * numpy.sum(numpy.power(combined_thetas, 2)) ZeroDivisionError: float division by zero

ImportError: No module named 'scipy'

I am facing this issue :

Traceback (most recent call last):
File "train.py", line 11, in
from scipy.optimize import fmin_l_bfgs_b
ImportError: No module named 'scipy'

ValueError("'arr' does not have a suitable array shape for any mode.") ERROR

We are getting the following error while executing autonomous.py in RPi 3 Model B v1.2

Traceback (most recent call last):
File "autonomous.py", line 92, in
main()
File "autonomous.py", line 89, in main
autonomous_control(model)
File "autonomous.py", line 27, in autonomous_control
direction = predictor.predict(stream)
File "/home/pi/test/mu/multunus/predict.py", line 33, in predict
input_layer_size, number_of_labels, x_value = _convert_stream_to_array(stream)
File "/home/pi/test/mu/multunus/predict.py", line 58, in _convert_stream_to_array
resized_image_array = imresize(image_array, IMAGE_DIMENSIONS)
File "/usr/lib/python3/dist-packages/scipy/misc/pilutil.py", line 421, in imresize
im = toimage(arr, mode=mode)
File "/usr/lib/python3/dist-packages/scipy/misc/pilutil.py", line 238, in toimage
raise ValueError("'arr' does not have a suitable array shape for any mode.")
ValueError: 'arr' does not have a suitable array shape for any mode.

What could the mistake from our side???

ERROR PLEASE HELP

Traceback (most recent call last):
File "/home/pi/interactive_control_train.py", line 6, in
import configuration
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 305, in _custom_import
module = self._original_import(*args, **kw)
ImportError: No module named 'configuration'

UnicodeDecodeError while running autonomous.py

I am facing UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 9: ordinal not in range(128) error, I have placed the traned module from this same repo in optimized teta folder

Traceback (most recent call last):
File "autonomous.py", line 92, in
main()
File "autonomous.py", line 89, in main
autonomous_control(model)
File "autonomous.py", line 14, in autonomous_control
predictor = Predictor(model)
File "/home/pi/Desktop/rcar/predict.py", line 23, in init
self._open_model_file(model_file)
File "/home/pi/Desktop/rcar/predict.py", line 27, in _open_model_file
self.model = pickle.load(mod)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 9: ordinal not in range(128)

can't work the motors

in the setup section. The images for training are captured using interactive_control_train.py, the car can't be controlled using the direction arrows..

issue after training data model

I have trained model with following inputs
python train.py 0.1 60
python train.py 0.5 114
python train.py 0.5 90
but car remains idle it doesn't move forward, left, right it just stays idle.....
I'm doing something wrong?
you can download file here

Plan to add new features

We are implementing the same project, there is another repo here. As that repo uses the HAAR cascade method to detect traffic signals and stops signs but the issue is that repo uses the client-server method which sacrifices the framerate of cam stream and other processing resources . We needed your help to modify the code and add features of detect sign boards and other stuff.

Issue in training data set and trained model

I pasted the images u gave from dataset i.e https://s3.amazonaws.com/multunus-machine-learning/autonomous-rc-car-data-set.tar.gz .....but its giving error
-----------------------------start----------------------------------------------------------------
Loading images to array...
/home/pi/Desktop/auto1/sigmoid.py:7: RuntimeWarning: overflow encountered in power
return 1.0/(1.0 + power(e, -x_value))
^CTraceback (most recent call last):
File "train.py", line 116, in
main()
File "train.py", line 109, in main
function_parameters)
File "train.py", line 72, in minimize_cost_function
fprime=gradients_wrapper, args=[function_parameters])
File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 186, in fmin_l_bfgs_b
**opts)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 314, in minimize_lbfgsb
f, g = func_and_grad(x)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/lbfgsb.py", line 266, in func_and_grad
g = jac(x, *args)
File "train.py", line 37, in gradients_wrapper
return gradients(gradient_parameters)
File "/home/pi/Desktop/auto1/cost_function.py", line 59, in gradients
hidden_layer_input = numpy.c
[numpy.ones(input_examples_size), x_values].dot(theta_1.T)
File "/usr/lib/python2.7/dist-packages/numpy/lib/index_tricks.py", line 326, in getitem
res = _nx.concatenate(tuple(objs), axis=self.axis)
-----------------------------------------------end-----------------------------------------

Then i got your trained model from https://s3.amazonaws.com/multunus-machine-learning/model_2016-07-20_19-38-07_l0.05_h114.pkl. and then executed it but it still gives error
note: i renamed the downloaded .pkl file as model.pkl
-----------------------------------start----------------------------------------------------
./optimized_thetas/model.pkl

Traceback (most recent call last):
File "autonomous.py", line 92, in
main()
File "autonomous.py", line 89, in main
autonomous_control(model)
File "autonomous.py", line 14, in autonomous_control
predictor = Predictor(model)
File "/home/pi/Desktop/auto1/predict.py", line 23, in init
self._open_model_file(model_file)
File "/home/pi/Desktop/auto1/predict.py", line 28, in _open_model_file
self.thetas = self.model['optimized_theta']
KeyError: 'optimized_theta'
----------------------------------------end----------------------------------------------------
need help in it pls ASAP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.