This is an old revision of the document!
Table of Contents
How to Program Darwin OP 2 to Walk Backward
Author: Yu Hang He Email: hey6@unlv.nevada.edu
Date: Last modified on <11/20/17>
Keywords: Darwin OP 2, C Programming, Robotis OP 2
This tutorial will demonstrate how to program Darwin OP 2 to walk backward. The walking algorithm for backward motion is included in Darwin OP 2's source code by Robotis. However, there is no on-line, or barely any, documentation of this process. This tutorial take approximately 1 hour to complete.
Motivation and Audience
This tutorial's motivation is to demonstrate how to program Darwin OP 2 to walk toward a target. This tutorial assumes the reader has the following background and interests:
- Familiar with handling Darwin OP 2
- Familiar with Cplusplus/C programming language
- Familiar with Cplusplus/C codes on Darwin OP 2
The rest of this tutorial is presented as follows:
Programming
Following a target can be accomplished through BallFollower Class. There is a tutorial on Robotis' online manual that may provide further detail.
This tutorial will only demonstrate parts of program that needs modification to include program Darwin OP 2 to walk toward a target.
- | main.cpp
#include <stdio.h> #include <unistd.h> #include <string.h> #include <libgen.h> #include "LinuxDARwIn.h" #define INI_FILE_PATH "../../../../Data/config.ini" #define U2D_DEV_NAME "/dev/ttyUSB0" using namespace Robot; void change_current_dir() { char exepath[1024] = {0}; if(readlink("/proc/self/exe", exepath, sizeof(exepath)) != -1) chdir(dirname(exepath)); } int main(void) { printf( "\n===== Walk Backward Tutorial for DARwIn =====\n\n"); change_current_dir(); minIni* ini = new minIni(INI_FILE_PATH); //////////////////// Framework Initialize //////////////////////////// LinuxCM730 linux_cm730(U2D_DEV_NAME); CM730 cm730(&linux_cm730); if(MotionManager::GetInstance()->Initialize(&cm730) == false) { printf("Fail to initialize Motion Manager!\n"); return 0; } MotionManager::GetInstance()->LoadINISettings(ini); Walking::GetInstance()->LoadINISettings(ini); MotionManager::GetInstance()->AddModule((MotionModule*)Head::GetInstance()); MotionManager::GetInstance()->AddModule((MotionModule*)Walking::GetInstance()); LinuxMotionTimer *motion_timer = new LinuxMotionTimer(MotionManager::GetInstance()); motion_timer->Start(); ///////////////////////////////////////////////////////////////////// int n = 0; int param[JointData::NUMBER_OF_JOINTS * 5]; int wGoalPosition, wStartPosition, wDistance; for(int id=JointData::ID_R_SHOULDER_PITCH; id<JointData::NUMBER_OF_JOINTS; id++) { wStartPosition = MotionStatus::m_CurrentJoints.GetValue(id); wGoalPosition = Walking::GetInstance()->m_Joint.GetValue(id); if( wStartPosition > wGoalPosition ) wDistance = wStartPosition - wGoalPosition; else wDistance = wGoalPosition - wStartPosition; wDistance >>= 2; if( wDistance < 8 ) wDistance = 8; param[n++] = id; param[n++] = CM730::GetLowByte(wGoalPosition); param[n++] = CM730::GetHighByte(wGoalPosition); param[n++] = CM730::GetLowByte(wDistance); param[n++] = CM730::GetHighByte(wDistance); } cm730.SyncWrite(MX28::P_GOAL_POSITION_L, 5, JointData::NUMBER_OF_JOINTS - 1, param); printf("Press the ENTER key to begin!\n"); getchar(); Head::GetInstance()->m_Joint.SetEnableHeadOnly(true, true); Walking::GetInstance()->m_Joint.SetEnableBodyWithoutHead(true, true); MotionManager::GetInstance()->SetEnable(true); while(1) { if(Action::GetInstance()->IsRunning() == 0) { Head::GetInstance()->m_Joint.SetEnableHeadOnly(true, true); Walking::GetInstance()->m_Joint.SetEnableBodyWithoutHead(true, true); if(Walking::GetInstance()->IsRunning() == false){ Walking::GetInstance()->X_MOVE_AMPLITUDE = -15.0; Walking::GetInstance()->A_MOVE_AMPLITUDE = 0.0; Walking::GetInstance()->Start(); } Walking::GetInstance()->X_MOVE_AMPLITUDE = -15.0; Walking::GetInstance()->A_MOVE_AMPLITUDE = 0.0; } } return 0; }
- | MakeFile
TARGET = walk_backward INCLUDE_DIRS = -I../../../include -I../../../../Framework/include CXX = g++ CXXFLAGS += -O2 -DLINUX -Wall $(INCLUDE_DIRS) #CXXFLAGS += -O2 -DDEBUG -DLINUX -Wall $(INCLUDE_DIRS) LFLAGS += -lpthread -ljpeg -lrt OBJECTS = main.o all: $(TARGET) clean: rm -f *.a *.o $(TARGET) core *~ *.so *.lo libclean: make -C ../../../build clean distclean: clean libclean darwin.a: make -C ../../../build $(TARGET): darwin.a $(OBJECTS) $(CXX) $(CFLAGS) $(OBJECTS) ../../../lib/darwin.a -o $(TARGET) $(LFLAGS) chmod 755 $(TARGET) # useful to make a backup "make tgz" tgz: clean mkdir -p backups tar czvf ./backups/ball_following_`date +"%Y_%m_%d_%H.%M.%S"`.tgz --exclude backups *
Demonstration
In this demonstration, I created a new mode called Sprint mode following the previous step and successfully execute it on Darwin OP 2.
Final Words
This tutorial's objective was to demonstrate how to program Darwin OP 2 to walk toward target.
For questions, clarifications, etc, Email: hey6@unlv.nevada.edu