PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
abdrive360.c
1 /*
2  @file abdrive360.c
3 
4  @author Parallax Inc
5 
6  @copyright
7  Copyright (C) Parallax Inc. 2017. All Rights MIT Licensed. See end of file.
8 
9  @brief
10 */
11 
12 
13 #include "abdrive360.h"
14 
15 
16 // // //
17 
18 
19 int abdrive360_getEepromPins();
20 int abdrive360_getEepromTransfer();
21 
22 volatile int abd360_initialized = 0;
23 
24 volatile int abd360_unitsPerRev = ABD360_UNITS_REV;
25 
26 volatile int abd360_pinCtrlLeft = ABD60_PIN_CTRL_L;
27 volatile int abd360_pinCtrlRight = ABD360_PIN_CTRL_R;
28 volatile int abd360_pinFbLeft = ABD60_PIN_FB_L;
29 volatile int abd360_pinFbRight = ABD360_PIN_FB_R;
30 
31 volatile int abd360_speedLimit = ABD_SPEED_LIMIT;
32 volatile int abd360_rampStep = ABD_RAMP_STEP;
33 volatile int abd360_speedLimitGoto = ABD_GOTO_SPEED_LIMIT;
34 volatile int abd360_rampStepGoto = ABD_GOTO_RAMP_STEP;
35 
36 volatile int abd360_gotoMode = ABD360_GOTO_BLOCK;
37 
38 volatile int abd360_suppress_eeprom = 0;
39 
40 void drive_suppress_eeprom(int state)
41 {
42  abd360_suppress_eeprom = state;
43 }
44 
45 
46 void drive_init(void)
47 {
48 
49  if(!abd360_suppress_eeprom)
50  {
51  abdrive360_getEepromPins();
52  }
53 
54  int result, flag = 0;
55  do
56  {
57  result = servo360_connect(abd360_pinCtrlLeft, abd360_pinFbLeft);
58  if(result < 0) flag = 1;
59  }
60  while(result < 0);
61 
62  do
63  {
64  result = servo360_connect(abd360_pinCtrlRight, abd360_pinFbRight);
65  if(result < 0) flag = 1;
66  }
67  while(result < 0);
68 
69  if(flag) pause(200); // PWR 1 -> 2, let servos engergize
70 
71  servo360_setUnitsFullCircle(abd360_pinCtrlLeft, ABD360_UNITS_REV);
72  servo360_setUnitsFullCircle(abd360_pinCtrlRight, ABD360_UNITS_REV);
73 
74  servo360_setAcceleration(abd360_pinCtrlLeft, abd360_rampStep * 50);
75  servo360_setAcceleration(abd360_pinCtrlRight, abd360_rampStep * 50);
76 
77  servo360_setMaxSpeed(abd360_pinCtrlLeft, abd360_speedLimit);
78  servo360_setMaxSpeed(abd360_pinCtrlRight, abd360_speedLimit);
79 
80  servo360_couple(abd360_pinCtrlLeft, abd360_pinCtrlRight);
81 
82  /*
83  #define S360_KPA 5000
84  #define S360_KIA 150
85  #define S360_KDA 0
86  #define S360_POS_INTGRL_MAX 150
87  #define S360_SCALE_DEN_A 1000
88  */
89 
90  servo360_setControlSys(abd360_pinCtrlLeft, S360_SETTING_KPA, 5000); // KPV
91  servo360_setControlSys(abd360_pinCtrlRight, S360_SETTING_KPA, 5000); // KPV
92  servo360_setControlSys(abd360_pinCtrlLeft, S360_SETTING_KIA, 150); // KIV
93  servo360_setControlSys(abd360_pinCtrlRight, S360_SETTING_KIA, 150); // KIV
94  servo360_setControlSys(abd360_pinCtrlLeft, S360_SETTING_KDA, 0); // KDV
95  servo360_setControlSys(abd360_pinCtrlRight, S360_SETTING_KDA, 0); // KDV
96  servo360_setControlSys(abd360_pinCtrlLeft, S360_SETTING_IA_MAX, 150); // FB360_VEL_INTGRL_MAX
97  servo360_setControlSys(abd360_pinCtrlRight, S360_SETTING_IA_MAX, 150); // FB360_VEL_INTGRL_MAX
98 
99  if(!abd360_suppress_eeprom)
100  {
101  abdrive360_getEepromTransfer();
102  }
103 
104  abd360_initialized = 1;
105 }
106 
107 
108 int abdrive360_getEepromPins()
109 {
110  int eeAddr = _AB360_EE_Start_ + _AB360_EE_Pins_;
111  unsigned char pinInfo[16];
112 
113  for(int i = 0; i < 16; i++)
114  {
115  pinInfo[i] = ee_getByte(eeAddr + i);
116  /*
117  if(pinInfo[i] <= 'z' && pinInfo[i] >= ' ')
118  {
119  print("%c", pinInfo[i]);
120  }
121  else
122  {
123  print("[%d]", pinInfo[i]);
124  }
125  */
126  }
127 
128  if(pinInfo[0] == 's' && pinInfo[1] == 'p' && pinInfo[2] == 'L' && pinInfo[5] == 'R')
129  {
130  abd360_pinCtrlLeft = (int) pinInfo[3];
131  abd360_pinCtrlRight = (int) pinInfo[6];
132  }
133  /*
134  else
135  {
136  print("!!! AB360 CONTROL PIN SETTINGS NOT FOUND !!!\r");
137  }
138  */
139 
140  if(pinInfo[8] == 'e' && pinInfo[9] == 'p' && pinInfo[10] == 'L' && pinInfo[13] == 'R')
141  {
142  abd360_pinFbLeft = (int) pinInfo[11];
143  abd360_pinFbRight = (int) pinInfo[14];
144  }
145  /*
146  else
147  {
148  print("!!! AB360 FEEDBACK PIN SETTINGS NOT FOUND !!!\r");
149  }
150  */
151 }
152 
153 int abdrive360_getEepromTransfer()
154 {
155  //if(!abd_us) abd_us = CLKFREQ/1000000;
156 
157  unsigned char str[12];
158  ee_getStr(str, 12, _AB360_EE_Start_);
159  str[11] = 0;
160 
161  if(strcmp(str, "AB360 "))
162  {
163  //print("!!! AB360 SETTINGS NOT FOUND, RETURNING !!!\r");
164  return -1;
165  }
166  /*
167  else
168  for(int i = 0; i < 12; i++)
169  {
170  if(str[i] <= 'z' && str[i] >= ' ')
171  {
172  print("%c", str[i]);
173  }
174  else
175  {
176  print("[%d]", str[i]);
177  }
178  }
179  */
180 
181 
182  int mVccwL = ee_getInt(_AB360_EE_Start_ + _AB360_EE_mVccwL_);
183  int bVccwL = ee_getInt(_AB360_EE_Start_ + _AB360_EE_bVccwL_);
184  int mVcwL = ee_getInt(_AB360_EE_Start_ + _AB360_EE_mVcwL_);
185  int bVcwL = ee_getInt(_AB360_EE_Start_ + _AB360_EE_bVcwL_);
186 
187  int mVccwR = ee_getInt(_AB360_EE_Start_ + _AB360_EE_mVccwR_);
188  int bVccwR = ee_getInt(_AB360_EE_Start_ + _AB360_EE_bVccwR_);
189  int mVcwR = ee_getInt(_AB360_EE_Start_ + _AB360_EE_mVcwR_);
190  int bVcwR = ee_getInt(_AB360_EE_Start_ + _AB360_EE_bVcwR_);
191 
192  servo360_setTransferFunction(abd360_pinCtrlLeft, S360_SETTING_VM_CCW, mVccwL);
193  servo360_setTransferFunction(abd360_pinCtrlLeft, S360_SETTING_VB_CCW, bVccwL);
194  servo360_setTransferFunction(abd360_pinCtrlLeft, S360_SETTING_VM_CW, mVcwL);
195  servo360_setTransferFunction(abd360_pinCtrlLeft, S360_SETTING_VB_CW, -bVcwL );
196 
197  servo360_setTransferFunction(abd360_pinCtrlRight, S360_SETTING_VM_CCW, mVccwR );
198  servo360_setTransferFunction(abd360_pinCtrlRight, S360_SETTING_VB_CCW, bVccwR);
199  servo360_setTransferFunction(abd360_pinCtrlRight, S360_SETTING_VM_CW, mVcwR);
200  servo360_setTransferFunction(abd360_pinCtrlRight, S360_SETTING_VB_CW, -bVcwR);
201  /*
202  print("\r=== Summary ===\r");
203  print("mVccwL = %d\r", mVccwL);
204  print("bVccwL = %d\r", bVccwL);
205  print("mVcwL = %d\r", mVcwL);
206  print("bVcwL = %d\r", bVcwL);
207  print("mVccwR = %d\r", mVccwR);
208  print("bVccwR = %d\r", bVccwR);
209  print("mVcwR = %d\r", mVcwR);
210  print("bVcwR = %d\r", bVcwR);
211  */
212 }
213 
214 
215 
ee_getInt
int ee_getInt(int addr)
Get an int value from a certain address in the Propeller Chip's dedicated EEPROM. If you are fetching...
Definition: eeprom_getInt.c:24
servo360_setMaxSpeed
int servo360_setMaxSpeed(int pin, int speed)
Set the maximum servo speed in terms of degrees per second.
Definition: servo360_setMaxSpeed.c:17
ABD_RAMP_STEP
#define ABD_RAMP_STEP
This default corresponds 600 ticks/second^2 acceleration and can be adjusted with the drive_setAccele...
Definition: abdrive.h:101
ee_getByte
char ee_getByte(int addr)
Get a byte value from a certain address in the Propeller Chip's dedicated EEPROM.
Definition: eeprom_getByte.c:24
ABD_GOTO_SPEED_LIMIT
#define ABD_GOTO_SPEED_LIMIT
This default defines the speed limit when the drive_goto function is called and can be adjusted with ...
Definition: abdrive.h:122
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition: libws2812.c:125
_AB360_EE_Start_
#define _AB360_EE_Start_
ActivityBot EEPROM calibration data start address.
Definition: abcalibrate360.h:68
servo360_setUnitsFullCircle
int servo360_setUnitsFullCircle(int pin, int units)
Set the number of units in a full circle. By default, this value is 360. This function does not act...
Definition: servo360_setUnitsFullCircle.c:17
ABD_GOTO_RAMP_STEP
#define ABD_GOTO_RAMP_STEP
This default corresponds 200 ticks/second^2 acceleration and can be adjusted with the drive_setAccele...
Definition: abdrive.h:132
servo360_couple
int servo360_couple(int pinA, int pinB)
This function is used by the abdrive360 library to reduce the drive on a servo whose position is furt...
Definition: servo360_couple.c:17
servo360_setControlSys
int servo360_setControlSys(int pin, int constant, int value)
The servo360.h library is designed to use Proportional, Integral and Derivative (PID) control to main...
Definition: servo360_setControlSys.c:16
servo360_setAcceleration
int servo360_setAcceleration(int pin, int unitsPerSecSquared)
Set acceleration in terms of degrees per second squared. The default is 3600 degrees per second squar...
Definition: servo360_setAcceleration.c:17
servo360_connect
int servo360_connect(int pinControl, int pinFeedback)
Initializes a connection to a Parallax Feedback 360 servo. The current position of the servo during t...
Definition: servo360_connect.c:19
abdrive360.h
This library provides a simple set of functions for making the ActivityBot 360 go certain distances a...
ABD_SPEED_LIMIT
#define ABD_SPEED_LIMIT
The default drive_speed limit is +/-128 ticks/second. This can be adjusted at runtime with the drive_...
Definition: abdrive.h:112
ee_getStr
unsigned char * ee_getStr(unsigned char *s, int n, int addr)
Fetch a string of byte values starting at a certain address in Propeller Chip's dedicated EEPROM.
drive_suppress_eeprom
void drive_suppress_eeprom(int state)
Enables or disables ignore calibration settings stored in EEPROM. This feature allows an ActivityBo...
Definition: abdrive360.c:40