Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Yaroslav Demchenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<div align="center"><img src="https://github.com/user-attachments/assets/ec4122c9-606f-4953-a0cf-03f3bd8f9b3a" width="30%"></div>

<h1 align="center">OCServo library</h1>

<div align="center">

<a href="/LICENSE">![GitHub](https://img.shields.io/github/license/JarikDem-Bot/OCServo)</a>
<a href="https://github.com/JarikDem-Bot/OCServo">![Static badge](https://img.shields.io/badge/OCServo-green)</a>
<a href="https://www.arduino.cc/">![Static badge](https://img.shields.io/badge/Arduino-blue)</a>

</div>

This library allows you to easily control servomotors of ROBS series of [OCServo manufacturer](https://en.ocservo.com/).

## Installation

[Tutorial with screen-shots how to download a library from GitHub and installing a ZIP-library](https://forum.arduino.cc/t/tutorial-with-screen-shots-how-to-download-a-library-from-github-and-installing-a-zip-library/994122/1)

## Connection

Connected using UART to RS485 board included to servomotor.
| Arduino | Servo Board |
| ----- | --- |
| Tx1* | Tx |
| Rx1* | Rx |
| 5V | 5V |
| GND | GND |

\* I used Hardware Serial port 1, but library supports any** Hardware or Software serial port.

\** Default baud rate value for servo is 1M, so port must be able to handle this value.

## Documentation
Detailed documentation can be found [->HERE<-](/docs/readme.md)

Also, check out [examples](/examples)!

## Tested on

Motors models:
- OCServo ROBS-301 servomotor

Boards:
- Arduino Mega 2560 Pro
- STM32F411 BlackPill ([Platformio](https://platformio.org/) and Arduino framework)

## Resources

- [OCServo documentations](https://en.ocservo.com/?page_id=8127)
- [ROBS-301 documentation](https://fcdn.goodq.top/caches/5bccc64925e01bd6f0a7519abd81e6a1/aHR0cDovL2VuLm9jc2Vydm8uY29tL3FmeS1jb250ZW50L3VwbG9hZHMvMjAyMy8wNS8xOWRmOTc4OTAxMDI0NGJlYjk0ZGE1YzQ3MWI0MTI0MC5wZGY_p_p100_p_3D.pdf)
- [OCServo store](https://en.ocservo.com/?page_id=15418&qfyuuid=-1&q_term=44&q_type=products)

## License

[MIT](/LICENSE)
143 changes: 143 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# OCServo documentation

__Content:__
- [Structures and Enums](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#structures-and-enums)
- [Constructors](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#constructors)
- [Instructions](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#instructions)
- [Set Values](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#set-values)
- [Get Values](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#get-values)
- [Other](https://github.com/JarikDem-Bot/OCServo/edit/master/docs/readme.md#other)

## Structures and Enums

- `OCSMode` - enum
- `OCS_SERVO` - Servo mode (0-360 degrees)
- `OCS_MOTOR` - Constant torque output mode
- `OCSResponseLevel` - enum
- `OCS_RESPOND_READ_ONLY` - Servo responds only to read and ping commands
- `OCS_RESPOND_ALL` - Servo responds to all commands
- `OCSResponse` - struct
- `byte prefix[2]` - Prefix `0xFF, 0xFF`
- `byte id` - ID of servo
- `byte length` - Length of instruction
- `byte instruction` - Instruction
- `int numberOfParameters` - Number of parameters
- `byte parameters[16]` - Supplementary control information except instruction
- `byte checksum` - Checksum = ~(ID+Length+Param1+...+ParamN)

## Constructors

- `OCServo (byte id, SoftwareSerial *serialPort)`
- `id` - ID of servomotor
- `serialPort` - SoftwareSerial port servo connected to
- `OCServo (byte id, HardwareSerial *serialPort)`
- `id` - ID of servomotor
- `serialPort` - HardwareSerial port servo connected to

## Instructions

- `void ping()`
- Used to read the work state of the servo
- `OCSResponse ocsRead(byte address, byte length)`
- `address` - Data address
- `length` - Read length
- Used to read the data inside the servo
- `OCSResponse ocsWrite(byte address, int paramsNumber, byte *params)`
- `address` - Data address
- `paramsNumber` - Number of parameters
- `*params` - Pointer to array of parameters
- This command is used to write parameters to the servo memory control table

## Set Values

- `OCSResponse setID(byte new_id)`
- `new_id` - New ID for servo (0-254)
- Changes ID of servomotor
- `void setBaudRate(long baudrate)`
- `baudrate` - New baudrate value
- Changes baudrate of servo (supported baud rates: 1M, 500000, 250000, 128000, 115200, 76800, 57600, 38400)
- `OCSResponse setMaxTorque(int torque)`
- `torque` - Torque value (0-1000)
- Sets max output torque (0 - min value, 1000 - max value)
- `OCSResponse setMode(int mode)`
- `mode` - `OCS_SERVO` or `OCS_MOTOR`
- Sets motor mode
- `OCSResponse setGoalAngle(int angle, long timeMillis= 0)`
- `angle` - Angle in degrees (0-360)
- `timeMillis=0` - Time to reach goal im ms (0-65535). Default is 0 (maximum speed)
- Sets goal angle for servo. It is possible to specify time in milliseconds.
- `OCSResponse setGoalPosition(int position, long timeMillis = 0)`
- `position` - Goal position (0-4095)
- `timeMillis` - Time to reach goal im ms (0-65535). Default is 0 (maximum speed)
- Sets goal angle for servo. It is possible to specify time in milliseconds. Same as setGoalAngle, but has more control of angle.
- `OCSResponse setOperationTime(long timeMillis)`
- `timeMillis` - Time to reach goal im ms (0-65535). Default is 0 (maximum speed)
- Sets time to reach goal. Will be overwritten by setGoalAngle or setGoalPosition.
- `OCSResponse setResponseDelay(int delayMicros)`
- `delayMicros` - Delay in microseconds (0-510)
- Sets response delay.
- `OCSResponse setResponseLevel(int level)`
- `level` - `OCS_RESPOND_READ_ONLY` or `OCS_RESPOND_ALL`
- Sets level of response.
- `OCSResponse setMinAngle(int angle)`
- `angle` - Angle (0-4095)
- Sets min angle of servo
- `OCSResponse setMaxAngle(int angle)`
- `angle` - Angle (0-4095)
- Sets max angle servo
- `OCSResponse setMaxVoltage(int voltage)`
- `voltage`
- .
- `OCSResponse setMinVoltage(int voltage)`
- `voltage`
- .
- `OCSResponse setOperationSpeed(long speed)`
- `speed` - Speed of a servo, AD/ms (0-65535)
- Sets the speed of the servo.

## Get Values

- `byte getID()`
- Returns ID of a servo
- Returns 255 if error occured
- `long getBaudRate()`
- Returns current baud rate of a servo
- Returns -1 if error occured
- `int getMaxTorque()`
- Returns max torque of a servo
- Returns -1 if error occured
- `int getMode()`
- Returns motor mode
- Returns -1 if error occured.
- `int getGoalAngle()`
- Returns goal angle (0-360 degrees)
- `int getGoalPosition()`
- Returns goal position (0-4095)
- `long getOperationTime()`
- Returns operation time in ms (0-65535)
- `int getResponseDelay()`
- Returns response delay in microseconds (0-510)
- `int getResponseLevel()`
- Returns response level (`OCS_RESPOND_READ_ONLY` or `OCS_RESPOND_ALL`)
- `int getMinAngle()`
- Returns min angle of servo (0-4095)
- `int getMaxAngle()`
- Returns max angle of servo (0-4095)
- `int getMaxVoltage()`
- .
- `int getMinVoltage()`
- .
- `long getOperationSpeed()`
- Returns operation speed in AD/ms (0-65535)
- `bool reachedGoal()`
- Returns true if servo reached it's goal. Otherwise returns false

## Other

- `void begin(long baudrate=1000000)`
- `baudrate` - Baud rate value. Default is 1M
- Sets baudrate value for serial port servo connected to.
- Function `setBaudRate` calls this method inside, so you do not need to do this manually.
- `void printResponse(OCSResponse response)`
- `response` - `OCSResponse` struct with response.
- Prints response structure.
47 changes: 47 additions & 0 deletions examples/basicReadValues/basicReadValues.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Arduino.h>
#include <OCServo.h>

byte id = 0x02;
int goal = 0;
unsigned long lastPrint = 0;

OCServo servo(id, &Serial1);

void setup() {
Serial.begin(38400);

// Servo setup
servo.begin();
servo.setBaudRate(38400);

// Basic settings for servo mode
servo.setMode(OCS_SERVO);
OCSResponse fullResponse = servo.setMaxTorque(400);
}


void loop() {
if(servo.reachedGoal()) { // If the servo reached the goal
goal = 360 - goal; // Change the goal
servo.setGoalAngle(goal);
}

// Print all the data every second
if(millis() > lastPrint + 1000) {
Serial.print("ID: 0x"); Serial.println(servo.getID(), HEX);
Serial.print("Baud rate: "); Serial.println(servo.getBaudRate());
Serial.print("Max torque: "); Serial.println(servo.getMaxTorque());
Serial.print("Mode: "); Serial.println((servo.getMode() == OCS_SERVO) ? "SERVO" : "MOTOR");
Serial.print("Goal Angle: "); Serial.println(servo.getGoalAngle());
Serial.print("Goal Position: "); Serial.println(servo.getGoalPosition());
Serial.print("Operation Time: "); Serial.println(servo.getOperationTime());
Serial.print("Response Delay: "); Serial.println(servo.getResponseDelay());
Serial.print("Response level: "); Serial.println((servo.getResponseLevel() == OCS_RESPOND_READ_ONLY) ? "READ ONLY" : "ALL");
Serial.print("Min Angle: "); Serial.println(servo.getMinAngle());
Serial.print("Max Angle: "); Serial.println(servo.getMaxAngle());
Serial.print("Min Voltage: "); Serial.println(servo.getMinVoltage());
Serial.print("Max Voltage: "); Serial.println(servo.getMaxVoltage());
Serial.print("Operation Speed: ");Serial.println(servo.getOperationSpeed());
lastPrint = millis();
}
}
30 changes: 30 additions & 0 deletions examples/basicRotation/basicRotation.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <Arduino.h>
#include <OCServo.h>

byte id = 0x02;

OCServo servo(id, &Serial1);

void setup() {
// put your setup code here, to run once:
Serial.begin(38400);

// Servo setup
servo.begin();
servo.setBaudRate(38400);

// Basic settings for servo mode
servo.setMode(OCS_SERVO);
servo.setMaxTorque(200);
}


void loop() {
// Move the servo to 360 degrees for 3 seconds
servo.setGoalPosition(360, 3000);
delay(3000);

// Move the servo to 0 degrees for 3 seconds
servo.setGoalPosition(0, 3000);
delay(3000);
}
53 changes: 53 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#######################################
# Syntax Coloring Map
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

OCServo KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
setKeyId KEYWORD2
ping KEYWORD2
ocsRead KEYWORD2
ocsWrite KEYWORD2
setID KEYWORD2
setBaudRate KEYWORD2
setMaxTorque KEYWORD2
setMode KEYWORD2
setGoalAngle KEYWORD2
setGoalPosition KEYWORD2
setOperationTime KEYWORD2
setResponseDelay KEYWORD2
setResponseLevel KEYWORD2
setMinAngle KEYWORD2
setMaxAngle KEYWORD2
setMaxVoltage KEYWORD2
setMinVoltage KEYWORD2
setOperationSpeed KEYWORD2
getID KEYWORD2
getBaudRate KEYWORD2
getMaxTorque KEYWORD2
getMode KEYWORD2
getGoalAngle KEYWORD2
getGoalPosition KEYWORD2
getOperationTime KEYWORD2
getResponseDelay KEYWORD2
getResponseLevel KEYWORD2
getMinAngle KEYWORD2
getMaxAngle KEYWORD2
getMaxVoltage KEYWORD2
getMinVoltage KEYWORD2
getOperationSpeed KEYWORD2
reachedGoal KEYWORD2
begin KEYWORD2
printResponse KEYWORD2

######################################
# Constants (LITERAL1)
#######################################
OCSResponse LITERAL1
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=OCServo
version=1.0.1
author=JarikDem-Bot
maintainer=JarikDem-Bot
sentence=Allows user to control ROBS series servos (OCServo manufacturer).
paragraph=Allows user to control ROBS series servos by OCServo manufacturer.
category=Device Control
url=https://github.com/JarikDem-Bot/OCServo
architectures=*
Loading