eBoard ①⑧⑨
Written for SIA 2017/2018
eagle_ServoCds55.h
Go to the documentation of this file.
1 #ifndef EAGLE_EBOARD_HELPLIB_SERVOCDS55
2  #define EAGLE_EBOARD_HELPLIB_SERVOCDS55
3 
12 //=====================================================================================================================================================
13 // ServoCds55
14 //=====================================================================================================================================================
15 
16  //-------------------------------------------------------------------------------------------------------------------------------------------------
17  // class
18  //-------------------------------------------------------------------------------------------------------------------------------------------------
19 
40  struct ServoCds55 {
46  #if defined(__AVR_ATmega2560__)
47  ServoCds55(int CS=53);
48  #else
49  ServoCds55(int CS=10);
50  #endif
51  void begin();
59  void WritePos(int ID,int Pos);
65  void write(int ID,int Pos);
70  void setVelocity(int velocity);
76  void setPoslimit(int posLimit);
80  void rotate(int ID,int velocity);
87  void SetServoLimit(int ID,int upperLimit);
94  void SetMotormode(int ID, int velocity);
101  void SetID(int ID, int newID);
106  void Reset(int ID);
112  byte sendWait (const byte what);
119  };
120 
122 
123  //-------------------------------------------------------------------------------------------------------------------------------------------------
124  // definitions
125  //-------------------------------------------------------------------------------------------------------------------------------------------------
126 
127  ServoCds55::ServoCds55 (int CS):cs(CS) {
128  velocity_temp = 150;
129  upperLimit_temp = 300;
130  }
131 
132  void ServoCds55::setVelocity(int velocity){ //set servo velocity
133  velocity_temp = velocity;
134  }
135 
136  void ServoCds55::setPoslimit(int posLimit){ // set servo pos limit
137  upperLimit_temp = posLimit;
138  }
139 
140  inline void ServoCds55::rotate(int ID,int velocity){ // Motor Mode
141  SetServoLimit(ID,0);
142  delay(100);
143  SetMotormode(ID,velocity);
144  }
145  void ServoCds55::begin() {
146  pinMode(cs,OUTPUT);
147  digitalWrite(cs,HIGH);
148  SPI.begin ();
149  SPI.setClockDivider(SPI_CLOCK_DIV8);
150  }
151 
152  byte ServoCds55::sendWait (const byte what) {
153  byte a = SPI.transfer (what);
154  delayMicroseconds (20);
155  return a;
156  }
157 
158  void ServoCds55::write(int ID,int Pos){ // Servo Mode
160  WritePos(ID,Pos);// default velocity:150
161  }
162 
163  void ServoCds55::WritePos(int ID,int Pos){
164  int PosB = (Pos>>8 & 0xff);//low
165  int PosS = (Pos & 0xff);//high
166  int velocityB = (velocity_temp>>8 & 0xff);
167  int velocityS = (velocity_temp & 0xff);
168  digitalWrite(cs, LOW);
169  sendWait ('p'); sendWait (ID);
170  sendWait (PosB); sendWait (PosS);
171  sendWait (velocityB); sendWait (velocityS);
172  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
173  digitalWrite(cs, HIGH);
174  delay(10);
175  }
176 
177  void ServoCds55::SetServoLimit(int ID,int upperLimit_temp){
178  int upperLimitB = (upperLimit_temp>>8 & 0xff);
179  int upperLimitS = (upperLimit_temp & 0xff);
180  digitalWrite(cs, LOW);
181  sendWait ('s'); sendWait (ID);
182  sendWait (upperLimitB); sendWait (upperLimitS);
183  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
184  digitalWrite(cs, HIGH);
185  delay(10);
186  }
187 
188  void ServoCds55::SetMotormode(int ID, int velocity){
189  int velocityB = (velocity>>8 & 0xff);
190  int velocityS = (velocity & 0xff);
191  digitalWrite(cs, LOW);
192  sendWait ('m'); sendWait (ID);
193  sendWait (velocityB); sendWait (velocityS);
194  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
195  digitalWrite(cs, HIGH);
196  delay(10);
197  }
198 
199  void ServoCds55::SetID(int ID, int newID){
200  digitalWrite(cs, LOW);
201  sendWait ('i'); sendWait (ID);
202  sendWait (newID);
203  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
204  digitalWrite(cs, HIGH);
205  delay(10);
206  }
207 
208  void ServoCds55::Reset(int ID){
209  digitalWrite(cs, LOW);
210  sendWait ('r'); sendWait (ID);
211  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
212  digitalWrite(cs, HIGH);
213  delay(10);
214  }
215 
216 
218 
221 
222 #endif
void SetServoLimit(int ID, int upperLimit)
(probably) set the posLimit for only one servo (will be overwritten by write() => writePos()) ...
int velocity_temp
stores the posLimit value send with write()
void SetMotormode(int ID, int velocity)
(probably) set the velocity of only one Servo
void setVelocity(int velocity)
Sets the default speed of servos.
byte sendWait(const byte what)
sends data. This is used internally and shouldnt be used!
void begin()
begin the communication and setup SPI
ServoCds55(int CS=10)
The constructor.
#define SPI_CLOCK_DIV8
Definition: eagle_SPI.h:28
void rotate(int ID, int velocity)
makes nothing
void SetID(int ID, int newID)
change the ID of a special Servo
void WritePos(int ID, int Pos)
Moves a Servo to a certain position.
int upperLimit_temp
stores the posLimit value send with write()
void write(int ID, int Pos)
Moves a Servo to a certain position.
[SPI] This is used to communicate with the smart servo shield         Don't use manually ...
void Reset(int ID)
resets a servo
void setPoslimit(int posLimit)
Sets the position limits for the servos.
ServoCds55 _servoHandler
this is the "to_use" instance of ServoCds55
int optVAL_t
Definition: eBoard.h:137
optVAL_t cs
stores the posLimit value send with write()