-
Notifications
You must be signed in to change notification settings - Fork 34
/
BioloidEx.h
109 lines (89 loc) · 4.6 KB
/
BioloidEx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
BioloidController.h - ArbotiX Library for Bioloid Pose Engine
Copyright (c) 2008-2012 Michael E. Ferguson. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef BioloidEx_h
#define BioloidEx_h
/* poses:
* PROGMEM prog_uint16_t name[ ] = {4,512,512,482,542}; // first number is # of servos
* sequences:
* PROGMEM transition_t name[] = {{NULL,count},{pose_name,1000},...}
*/
#include <ax12.h>
/* pose engine runs at 30Hz (33ms between frames)
recommended values for interpolateSetup are of the form X*BIOLOID_FRAME_LENGTH - 1 */
#define BIOLOID_FRAME_LENGTH 20
/* we need some extra resolution, use 13 bits, rather than 10, during interpolation */
#define BIOLOID_SHIFT 3
/** a structure to hold transitions **/
typedef struct{
unsigned int * pose; // addr of pose to transition to
int time; // time for transition
} transition_t;
/** Bioloid Controller Class for mega324p/644p clients. **/
class BioloidControllerEx
{
public:
/* For compatibility with legacy code */
BioloidControllerEx(long baud); // baud usually 1000000
/* New-style constructor/setup */
BioloidControllerEx() {};
void setup(int servo_cnt);
/* Pose Manipulation */
void loadPose( const unsigned int * addr ); // load a named pose from FLASH
void readPose(); // read a pose in from the servos
void writePose(); // write a pose out to the servos
int getCurPose(int id); // get a servo value in the current pose
int getNextPose(int id); // get a servo value in the next pose
void setNextPose(int id, int pos); // set a servo value in the next pose
void setNextPoseByIndex(int index, int pos); // set a servo value by index for next pose
void setId(int index, int id); // set the id of a particular storage index
int getId(int index); // get the id of a particular storage index
/* Pose Engine */
void interpolateSetup(int time); // calculate speeds for smooth transition
int interpolateStep(boolean fWait=true); // move forward one step in current interpolation
unsigned char interpolating; // are we in an interpolation? 0=No, 1=Yes
unsigned char runningSeq; // are we running a sequence? 0=No, 1=Yes
int poseSize; // how many servos are in this pose, used by Sync()
// Kurt's Hacks
uint8_t frameLength; // Allow variable frame lengths, to test...
/* to interpolate:
* bioloid.loadPose(myPose);
* bioloid.interpolateSetup(67);
* while(bioloid.interpolating > 0){
* bioloid.interpolateStep();
* delay(1);
* }
*/
/* Sequence Engine */
void playSeq( const transition_t * addr ); // load a sequence and play it from FLASH
void play(); // keep moving forward in time
unsigned char playing; // are we playing a sequence? 0=No, 1=Yes
/* to run the sequence engine:
* bioloid.playSeq(walk);
* while(bioloid.playing){
* bioloid.play();
* }
*/
private:
unsigned int * pose_; // the current pose, updated by Step(), set out by Sync()
unsigned int * nextpose_; // the destination pose, where we put on load
int * speed_; // speeds for interpolation
unsigned char * id_; // servo id for this index
// unsigned long lastframe_; // time last frame was sent out
unsigned long nextframe_; //
transition_t * sequence; // sequence we are running
int transitions; // how many transitions we have left to load
};
#endif