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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Functions related to HSR Servos

//To use these functions properly, you need to copy and paste the code below 
//to your "main.c"
/*////////////////////////////////////////

#include "hsr_serial.c"

#int_rda
void serial_isr() 
{
  if(kbhit())
 	{
   	RxData[RxCounter]=getc();
    RxCounter++;
	RxCounter%=3;
	   if (RxCounter==0)
	   {
	     	RxLastData[0]=RxData[0];
			RxLastData[1]=RxData[1];
	        RxComplete=1;
	   }
	   else
	   {
	  		RxComplete=0;
	   }
 	}   
}


*/////////////////////////////////////////
#include <regloc.h> // Special Register Locations
#include <hsr_serial.h> // Special commands and memory locations for HSR servos 
void HSR_SerialInitialization(void)
{
   	//Disable Transmission and Receive
   	CREN=0;
   	TXEN=0;
    
  	//Configure both transmission and receive pins as input 
    TRISC |=0b11000000;
   
    enable_interrupts(int_rda);
    enable_interrupts(global);

}

//Release Servo
void HSR_Release(void)
{
   _RxComplete=0; //The data is not available

   _CheckSum=_HSR_ComStart+_HSR_Release;
   _CheckSum=(~_CheckSum)+1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_Release,0x00,0x00,_CheckSum); 
   CREN = 1;
   TXEN = 0;
   
   WaitRxDataReady();
  

}

//Release Servo
void HSR_Release_v2(void)
{
  
   _CheckSum=_HSR_ComStart+_HSR_Release;
   _CheckSum=(~_CheckSum)+1;
   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_Release,0x00,0x00,_CheckSum); 
   TXEN=0;

}

//This function reads the servo position 
uint16 HSR_ReadPosition(void)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadPosition;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN=0;
       TXEN=1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadPosition,0x00,0x00,_CheckSum);
       CREN=1;
       TXEN=0;

	   WaitRxDataReady();
       _ServoPosH = _RxLastData[0];
       _ServoPosL = _RxLastData[1];

       return(make16(_RxLastData[0],_RxLastData[1]));
       
}

//This function only starts to reading current position of servo.
//User need to be check if the data is ready using variable _RxComplete
void HSR_ReadPosition_v2(void)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadPosition;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN=0;
       TXEN=1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadPosition,0x00,0x00,_CheckSum);
       CREN=1;
       TXEN=0;
}

//This function returns if incoming data is ready (1), or not (0). 
short IsRxDataReady(void)
{
       return(_RxComplete);
}

//This function waits until incomming data is ready.

void WaitRxDataReady(void)
{   
	_Start_time =get_timer0();
    while(!_RxComplete)
	{ if (get_timer0()-_Start_time >_Reset_interval)
		{
			output_low(Pin_D5);
			output_low(Pin_D6);
			output_low(Pin_D7);
			break;
		}
	}
}

//Read a Specific EPPROM location
uint8 HSR_ReadEPPROM(uint8 addr)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadEPPROM+addr;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN=0;
       TXEN=1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadEPPROM,addr,0x00,_CheckSum);
       CREN=1;
       TXEN=0;
      
       WaitRxDataReady(); // Wait until receive is completed.
       return(_RxLastData[0]);
}

//This function only starts to reading from EPPROM.
//User need to be check if the data is ready using variable _RxComplete
void HSR_ReadEPPROM_v2(uint8 addr)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadEPPROM+addr;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN = 0;
       TXEN = 1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadEPPROM,addr,0x00,_CheckSum);
       CREN = 1;
       TXEN = 0;
     
}

//Write EPPROM
void HSR_WriteEPPROM(uint8 addr, uint8 data)
{
   _RxComplete=0; //The data is not available

   _CheckSum = _HSR_ComStart + _HSR_WriteEPPROM + addr + data;
   _CheckSum = (~_CheckSum) + 1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_WriteEPPROM,addr,data,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady();

}

//Write EPPROM
void HSR_WriteEPPROM_v2(uint8 addr, uint8 data)
{
  
   _CheckSum = _HSR_ComStart + _HSR_WriteEPPROM + addr + data;
   _CheckSum = (~_CheckSum) + 1;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_WriteEPPROM,addr,data,_CheckSum); 
   TXEN = 0;

}

//Read a Specific Memory location
uint8 HSR_ReadMemory(uint8 addr)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadMemory+addr;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN=0;
       TXEN=1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadMemory,addr,0x00,_CheckSum);
       CREN=1;
       TXEN=0;
      
       WaitRxDataReady(); // Wait until receive is completed.
       return(_RxLastData[0]);
}

//This function only starts to reading memory.
//User need to be check if the data is ready using variable _RxComplete
void HSR_ReadMemory_v2(uint8 addr)
{
 	   _RxComplete=0; //The data is not available
       _CheckSum=_HSR_ComStart+_HSR_ReadMemory+addr;
	   _CheckSum=(~_CheckSum)+1;
	   
       CREN=0;
       TXEN=1;
	   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadMemory,addr,0x00,_CheckSum);
       CREN=1;
       TXEN=0;
     
}

//Write Memory
void HSR_WriteMemory(uint8 addr, uint8 data)
{
   _RxComplete=0; //The data is not available

   _CheckSum=_HSR_ComStart+_HSR_WriteMemory+addr+data;
   _CheckSum=(~_CheckSum)+1;

   CREN=0;
   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_WriteMemory,addr,data,_CheckSum); 
   CREN=1;
   TXEN=0;

   WaitRxDataReady(); // Wait until receive is completed.

}

//Write Memory
void HSR_WriteMemory_v2(uint8 addr, uint8 data)
{
   _CheckSum=_HSR_ComStart+_HSR_WriteMemory+addr+data;
   _CheckSum=(~_CheckSum)+1;

   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_WriteMemory,addr,data,_CheckSum); 
   TXEN=0;

}

//Set target position
void HSR_SetPosition(uint8 PosH, uint8 PosL)
{
   
   _CheckSum = _HSR_ComStart + _HSR_SetPosition + PosH + PosL;
   _CheckSum=(~_CheckSum)+1;

   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SetPosition,PosH,PosL,_CheckSum); 
   TXEN=0;

}


//Overloaded Function
//Set target position
void HSR_SetPosition(uint8 ServoID, uint8 PosH, uint8 PosL)
{
   
   _CheckSum = _HSR_ComStart + ServoID + PosH + PosL;
   _CheckSum=(~_CheckSum)+1;

   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,ServoID,PosH,PosL,_CheckSum); 
   TXEN=0;

}

//This function is used to smoothly go to a target position 
//from limp position
void HSR_SetInitialPosition(uint8 PosH, uint8 PosL)
{

   HSR_ReadPosition_v2();//Read Current Position
   WaitRxDataReady(); // Wait until data is receieved
   HSR_WriteMemory(_HSR_MEM_CurTargetPosL, _ServoPosL);
   HSR_WriteMemory(_HSR_MEM_CurTargetPosH, _ServoPosH);
   HSR_SetPosition(PosH, PosL);

}

//Read Servo Version and ID
void HSR_ReadVerID(void)
{

   _RxComplete=0; //The data is not available

   _CheckSum=_HSR_ComStart+_HSR_ReadVerID;
   _CheckSum=(~_CheckSum)+1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadVerID,0x00,0x00,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady(); // Wait until receive is completed.
   _ServoID = _RxLastData[0];
   _ServoVersion = _RxLastData[1];
 
}

//Read Servo Version and ID. 
//The servo version and id will be available as the first and 
//second elements of _RxLastData at the end of communication. 
void HSR_ReadVerID_v2(void)
{

   _CheckSum=_HSR_ComStart+_HSR_ReadVerID;
   _CheckSum=(~_CheckSum)+1;

   TXEN=1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadVerID,0x00,0x00,_CheckSum); 
   TXEN=0;

}

//Read pulsewidth and voltage
void HSR_ReadPWM(void)
{

   _RxComplete = 0; //The data is not available

   _CheckSum = _HSR_ComStart + _HSR_ReadPWM;
   _CheckSum = (~_CheckSum) + 1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadPWM,0x00,0x00,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady(); // Wait until receive is completed.
   _ServoPW = _RxLastData[0];
   _ServoVoltage = _RxLastData[1];

}

//Read pulsewidth and voltage
//At te end of communication, pulsewidth and voltage will be 
//available as the first and second elements of _RxLastData,
//respectively.
void HSR_ReadPWM_v2(void)
{

   _CheckSum = _HSR_ComStart + _HSR_ReadPWM;
   _CheckSum = (~_CheckSum) + 1;

   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_ReadPWM,0x00,0x00,_CheckSum); 
   TXEN = 0;

}

//Set the speed of a single servo using its ID 
//and read its current position
void HSR_SetSpeed(uint8 ServoID, uint8 ServoSpeed)
{

   _RxComplete = 0; //The data is not available

   _CheckSum = _HSR_ComStart + _HSR_SetSpeed + ServoID + ServoSpeed;
   _CheckSum = (~_CheckSum) + 1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SetSpeed,ServoID,ServoSpeed,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady(); // Wait until receive is completed.
   _ServoPosH = _RxLastData[0];
   _ServoPosL = _RxLastData[1];

}

//Set the speed of a single servo using its ID 
//and read its current position.
//This function only starts the communication, 
//you need to wait communication to end successfully.
//Current position will be available as _RxLastData
void HSR_SetSpeed_v2(uint8 ServoID, uint8 ServoSpeed)
{

   _CheckSum = _HSR_ComStart + _HSR_SetSpeed + ServoID + ServoSpeed;
   _CheckSum = (~_CheckSum) + 1;

   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SetSpeed,ServoID,ServoSpeed,_CheckSum); 
   TXEN = 0;

}

//Select Control Parameter Set
void HSR_SelectControl(uint8 ControlSet)
{
// Note: ControlSet must be 1-3, otherwise it will be processed as 3.

   _RxComplete = 0; //The data is not available

   _CheckSum = _HSR_ComStart + _HSR_SelectControl + ControlSet;
   _CheckSum = (~_CheckSum) + 1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SelectControl,0x00,ControlSet,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady(); // Wait until receive is completed.

}

//Select Control Parameter Set
//This function only starts to communication. 
//To make sure that communication ends succesfully, 
//wait 3ms or call WaitRxDataReady();
void HSR_SelectControl_v2(uint8 ControlSet)
{
// Note: ControlSet must be 1-3, otherwise it will be processed as 3.


   _CheckSum = _HSR_ComStart + _HSR_SelectControl + ControlSet;
   _CheckSum = (~_CheckSum) + 1;

   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SelectControl,0x00,ControlSet,_CheckSum); 
   TXEN = 0;

}

//Set Go/Stop
void HSR_SetGoStop(uint8 GoStop)
{

   _RxComplete = 0; //The data is not available

   _CheckSum = _HSR_ComStart + _HSR_SetGoStop + GoStop;
   _CheckSum = (~_CheckSum) + 1;

   CREN = 0;
   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SetGoStop,GoStop,0x00,_CheckSum); 
   CREN = 1;
   TXEN = 0;

   WaitRxDataReady(); // Wait until receive is completed.	

}

//Set Go/Stop
//This function only starts to communication. 
//To make sure that communication ends succesfully, 
//wait 3ms or call WaitRxDataReady();
void HSR_SetGoStop_v2(uint8 GoStop)
{

   _CheckSum = _HSR_ComStart + _HSR_SetGoStop + GoStop;
   _CheckSum = (~_CheckSum) + 1;

   TXEN = 1;
   printf("%c%c%c%c%c",_HSR_ComStart,_HSR_SetGoStop,GoStop,0x00,_CheckSum); 
   TXEN = 0;	

}

//Set Action
void HSR_SetAction(sint16 TargetAngle, uint16 ActionTime)
{
    uint16 	CurServoPos;
    uint16 	TargetPos;
    uint8 	TargetPosH;
    uint8	TargetPosL;
    uint16  ActionSpeed;
    uint8   ActionSpeedByte;
	CurServoPos = HSR_ReadPosition();
    TargetPos = TargetAngle+14500;
    TargetPos = TargetPos/10;
    TargetPosH = make8(TargetPos,1);
	TargetPosL = make8(TargetPos,0);
    ActionTime=ActionTime/10;

    if (TargetPos>CurServoPos)
	{
        ActionSpeed = (TargetPos-CurServoPos)/10;
        ActionSpeed*=39;
        ActionSpeed/=ActionTime;
 	} 	
    else
	{
		ActionSpeed = (CurServoPos-TargetPos)/10;
        ActionSpeed*=39;
        ActionSpeed/=ActionTime;
 	} 	
    ActionSpeedByte=make8(ActionSpeed,0);
    output_b(ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_Speed, ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_CurSpeed, ActionSpeedByte);
    HSR_SetPosition(TargetPosH,TargetPosL);
        
}

//Set Action
void HSR_SetAction_v2(sint16 TargetAngle, uint16 ActionTime)
{
    uint16 	CurServoPos;
    uint16 	TargetPos;
    uint8 	TargetPosH;
    uint8	TargetPosL;
    uint32  ActionSpeed;
    uint8   ActionSpeedByte;
	CurServoPos = HSR_ReadPosition();
    TargetPos = TargetAngle+14500;
    TargetPos = TargetPos/10;
    TargetPosH = make8(TargetPos,1);
	TargetPosL = make8(TargetPos,0);

    if (TargetPos>CurServoPos)
	{
        ActionSpeed = (TargetPos-CurServoPos);
        ActionSpeed*=2000;
        ActionSpeed/=ActionTime;
		ActionSpeed/=79;
 	} 	
    else
	{
		ActionSpeed = (CurServoPos-TargetPos);
        ActionSpeed*= 2000;
        ActionSpeed/=ActionTime;
		ActionSpeed/=79;
 	} 	
    ActionSpeedByte=make8(ActionSpeed,0);
    output_b(ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_Speed, ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_CurSpeed, ActionSpeedByte);
    HSR_SetPosition(TargetPosH,TargetPosL);
        
}

//Set Action and estimaate time of motion
uint32 HSR_SetAction_v3(sint16 TargetAngle, uint16 ActionTime)
{
    uint16 	CurServoPos;
    uint16 	TargetPos;
    uint8 	TargetPosH;
    uint8	TargetPosL;
    uint32  ActionSpeed;
    uint32  EstimatedTime;
    uint8   ActionSpeedByte;
	CurServoPos = HSR_ReadPosition();
    TargetPos = TargetAngle+14500;
    TargetPos = TargetPos/10;
    TargetPosH = make8(TargetPos,1);
	TargetPosL = make8(TargetPos,0);

    if (TargetPos>CurServoPos)
	{
        ActionSpeed = (TargetPos-CurServoPos);
        ActionSpeed*=2000;
        ActionSpeed/=79;
        EstimatedTime=ActionSpeed;
        ActionSpeed/=ActionTime;
		
 	} 	
    else
	{
		ActionSpeed = (CurServoPos-TargetPos);
        ActionSpeed*= 2000;
        ActionSpeed/=79;        
 		EstimatedTime=ActionSpeed;
		ActionSpeed/=ActionTime;
 	} 	
    ActionSpeedByte=make8(ActionSpeed,0);
    EstimatedTime/=ActionSpeedByte;
    output_b(ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_Speed, ActionSpeedByte);
    HSR_WriteMemory(_HSR_MEM_CurSpeed, ActionSpeedByte);
    HSR_SetPosition(TargetPosH,TargetPosL);
    return(EstimatedTime);
        
}