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 
23  struct ServoCds55 {
29  #if defined(__AVR_ATmega2560__)
30  ServoCds55(int CS=53);
31  #else
32  ServoCds55(int CS=10);
33  #endif
34  void begin();
42  void WritePos(int ID,int Pos);
48  void write(int ID,int Pos);
53  inline void setVelocity(int velocity);
59  inline void setPoslimit(int posLimit);
63  inline void rotate(int ID,int velocity);
70  void SetServoLimit(int ID,int upperLimit);
77  void SetMotormode(int ID, int velocity);
84  void SetID(int ID, int newID);
89  void Reset(int ID);
95  byte sendWait (const byte what);
102  };
103 
105  ServoCds55::ServoCds55 (int CS):cs(CS) {
106  velocity_temp = 150;
107  upperLimit_temp = 300;
108  }
109 
110  void ServoCds55::setVelocity(int velocity){ //set servo velocity
111  velocity_temp = velocity;
112  }
113 
114  void ServoCds55::setPoslimit(int posLimit){ // set servo pos limit
115  upperLimit_temp = posLimit;
116  }
117 
118  inline void ServoCds55::rotate(int ID,int velocity){ // Motor Mode
119  SetServoLimit(ID,0);
120  delay(100);
121  SetMotormode(ID,velocity);
122  }
123  void ServoCds55::begin() {
124  pinMode(cs,OUTPUT);
125  digitalWrite(cs,HIGH);
126  SPI.begin ();
127  SPI.setClockDivider(SPI_CLOCK_DIV8);
128  }
129 
130  byte ServoCds55::sendWait (const byte what) {
131  byte a = SPI.transfer (what);
132  delayMicroseconds (20);
133  return a;
134  }
135 
136  void ServoCds55::write(int ID,int Pos){ // Servo Mode
138  WritePos(ID,Pos);// default velocity:150
139  }
140 
141  void ServoCds55::WritePos(int ID,int Pos){
142  int PosB = (Pos>>8 & 0xff);//low
143  int PosS = (Pos & 0xff);//high
144  int velocityB = (velocity_temp>>8 & 0xff);
145  int velocityS = (velocity_temp & 0xff);
146  digitalWrite(cs, LOW);
147  sendWait ('p'); sendWait (ID);
148  sendWait (PosB); sendWait (PosS);
149  sendWait (velocityB); sendWait (velocityS);
150  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
151  digitalWrite(cs, HIGH);
152  delay(10);
153  }
154 
155  void ServoCds55::SetServoLimit(int ID,int upperLimit_temp){
156  int upperLimitB = (upperLimit_temp>>8 & 0xff);
157  int upperLimitS = (upperLimit_temp & 0xff);
158  digitalWrite(cs, LOW);
159  sendWait ('s'); sendWait (ID);
160  sendWait (upperLimitB); sendWait (upperLimitS);
161  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
162  digitalWrite(cs, HIGH);
163  delay(10);
164  }
165 
166  void ServoCds55::SetMotormode(int ID, int velocity){
167  int velocityB = (velocity>>8 & 0xff);
168  int velocityS = (velocity & 0xff);
169  digitalWrite(cs, LOW);
170  sendWait ('m'); sendWait (ID);
171  sendWait (velocityB); sendWait (velocityS);
172  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
173  digitalWrite(cs, HIGH);
174  delay(10);
175  }
176 
177  void ServoCds55::SetID(int ID, int newID){
178  digitalWrite(cs, LOW);
179  sendWait ('i'); sendWait (ID);
180  sendWait (newID);
181  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
182  digitalWrite(cs, HIGH);
183  delay(10);
184  }
185 
186  void ServoCds55::Reset(int ID){
187  digitalWrite(cs, LOW);
188  sendWait ('r'); sendWait (ID);
189  sendWait ('\t'); sendWait ('\r'); sendWait ('\n');
190  digitalWrite(cs, HIGH);
191  delay(10);
192  }
193 
194 
196 #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:16
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.
int optVAL_t
Definition: eBoard.h:196
optVAL_t cs
stores the posLimit value send with write()