HCOARC Arduino library for Obstacle Avoidance Robot Car Kit

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 725
Joined: Sun Aug 05, 2012 4:02 pm

HCOARC Arduino library for Obstacle Avoidance Robot Car Kit

Post by admin » Fri Sep 14, 2018 8:28 am

Image





This Arduino compatible library has been written specifically for the Hobby Components Obstacle Avoidance Robot Car Kit (HCKITS0060). It is used the demo sketch shipped with the kit and also provides an easy means of controlling the robot car using your own sketches. The library includes functions for controlling the robot cars motors and for taking reading from the ultrasonic sensor.

You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/



Using the HCOARC library

To use the library just include the HCOARC.h header file and then create an instance of the library. E.g:
  1. #include "HCOARC.h"
  2. HCOARC HCOARC;

To initialise the library place the following line in the Setup() loop at the top of the sketch:
  1. HCMotor.Init();


The following functions are available with this library:

  1. void Attach(MotorNum,  PinA,  PinB);
Attaches a PWM output for a standard DC motor where:
MotorNum is a value used to reference which motor you wish to attach. Valid values are:

LEFT_MOTOR - Attach the left motor (motor 0)
RIGHT_MOTOR - Attach the right motor (motor 1)

PinA is the digital pin number connected to INA of the H-Bride module.
PinB is the digital pin number connected to INB of the H-Bride module.


  1. void Detach(MotorNum);
Removes an attached motor from the list and sets its pin(s) to inputs where:

MotorNum the motor you wish to detach. Valid values are:
LEFT_MOTOR - Detach the left motor (motor 0)
RIGHT_MOTOR - Detach the right motor (motor 1)


  1. void Speed(MotorNum, Percent);
Sets the speed of one of the connected motors in % where:

MotorNum is the motor you wish to change the speed for. Valid values are:
LEFT_MOTOR - Change the speed for the left motor
RIGHT_MOTOR - Change the speed for the right motor

Percent is the motor speed specified in percent where 0 - full stop and 100 is full speed.



  1. void Direction(MotorNum, Direction);   
Sets the rotational direction of one of the connected motors where:

MotorNum is the motor you wish to change the speed for. Valid values are:
LEFT_MOTOR - Change the direction for the left motor
RIGHT_MOTOR - Change the direction for the right motor

Direction is the rotational direction. Valida values are
FORWARD - Motor will rotate in the forward direction.
REVERSE - Motor will rotate in the reverse direction.



  1. void Ultrasonic_Init(Echo, Trig);
Starts repeated measurements of the ultrasonic sensor where:

Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.



  1. void Ultrasonic_Init(Echo, Trig);
Starts repeated measurements of the ultrasonic sensor where:

Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.



  1. void Ultrasonic_Init(Power, Gnd, Echo, Trig);  
Starts repeated measurements of the ultrasonic sensor where:

Power is the digital pin number connected to the Power pin on the sensor.
Gnd is the digital pin number connected to the Gnd pin on the sensor.
Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.



  1. volatile uint8_t Ultrasonic_Raw(void);
Returns a raw distance measurement from the ultrasonic sensor measured in
100us ticks as type Byte,




Image

  1. /* FILE:    OARC_Example.h
  2.    DATE:    13/09/18
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.    
  6. 13/09/18 version 0.1: Original version
  7.  
  8. This is the demo sketch which is pre-flashed into the Nano shipped in the
  9. Hobby Components Obsicle Avoidance Robot Car Kit (HCKITS0060). The sketch is
  10. provided as an example of how to use the HCOARC library created specifically for this
  11. kit. You can find more information about this library on our support forum here:
  12.  
  13. HTTPS://HOBBYCOMPONENTS.COM/OARC
  14.  
  15. You may copy, alter and reuse this code in any way you like, but please leave
  16. reference to HobbyComponents.com in your comments if you redistribute this code.
  17. This software may not be used directly for the purpose of selling products that
  18. directly compete with Hobby Components Ltd's own range of products.
  19. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  20. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  21. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  22. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  23. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  24. REASON WHATSOEVER.
  25. */
  26.  
  27. #include "HCOARC.h"
  28.  
  29. HCOARC HCOARC;
  30.  
  31. #define LED_PIN 13
  32.  
  33. void setup()
  34. {
  35.   HCOARC.Init();
  36.   HCOARC.Ultrasonic_Init(A2, A5, A4, A3);
  37.  
  38.   HCOARC.Attach(LEFT_MOTOR, 2, 3);
  39.   HCOARC.Attach(RIGHT_MOTOR, 4, 5);
  40.  
  41.   pinMode(LED_PIN, OUTPUT);
  42.  
  43.   for(byte i = 0; i < 5; i++)
  44.   {
  45.     delay(500);
  46.     digitalWrite(LED_PIN, HIGH);
  47.     delay(500);
  48.     digitalWrite(LED_PIN, LOW);
  49.   }
  50.  
  51.   HCOARC.Speed(LEFT_MOTOR, 100);
  52.   HCOARC.Speed(RIGHT_MOTOR, 100);
  53. }
  54.  
  55.  
  56.  
  57. void loop()
  58. {
  59.   byte Distance = HCOARC.Ultrasonic_Raw();
  60.  
  61.   if(Distance >= 25)
  62.   {
  63.     HCOARC.Direction(LEFT_MOTOR, FORWARD);
  64.     HCOARC.Direction(RIGHT_MOTOR, FORWARD);
  65.   }else
  66.   {
  67.     digitalWrite(LED_PIN, HIGH);
  68.     HCOARC.Direction(LEFT_MOTOR, REVERSE);
  69.     HCOARC.Direction(RIGHT_MOTOR, FORWARD);
  70.  
  71.     delay(100);
  72.     digitalWrite(LED_PIN, LOW);
  73.   }
  74. }


Image

HCOARC.zip



Disclaimer: Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

Post Reply