control_law.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include "globals.h"
15 #include "arm_math.h"
16 #include "drivers/d_quadEnc.h"
17 #include "control_law.h"
18 
19 
20 void init_control(void)
21 {
22 #ifdef CONF_BOARD_QUAD_ENCODER
24  {
25  quadEncInit();
26  }
27 #endif
28 }
29 
30 
32 {
33 #ifdef CONF_BOARD_QUAD_ENCODER // Encoder Rx =======================================================
34 
36  { // Wheel encoders disabled
37  return;
38  }
39 
40  int chL, chR;
41  bool dirL, dirR;
42  int n=0;
43  int speedL, speedR;
44  static int encoderSendTimeMs=0;
45  static wheel_encoder_t wheelEncoderLast = {0};
46 
47  if(abs(g_comm_time_ms-encoderSendTimeMs)>=20)
48  { // Send data at 50Hz
49  encoderSendTimeMs = g_comm_time_ms;
50 
51  // Call read encoders
52  quadEncReadPositionAll(&chL, &dirL, &chR, &dirR);
53  quadEncReadSpeedAll((uint32_t*)&speedL, (uint32_t*)&speedR);
55 
56  // Set velocity direction
57  if(dirL)
58  {
59  speedL = -speedL;
60  }
61  if(dirR)
62  {
63  speedR = -speedR;
64  }
65 
66  // Convert encoder ticks to radians.
67  g_wheelEncoder.theta_l = (chL * g_flashCfg->encoderTickToWheelRad)/2; /*Division by 2 to account for 4x encoding*/
69 
70  // Convert TC pulse period to rad/sec. 20us per TC LSB x 2 (measure rising to rising edge).
71  if(speedL)
72  {
73  g_wheelEncoder.omega_l = g_flashCfg->encoderTickToWheelRad / (0.00002f * (float)speedL);
74  }
75  else
76  {
77  g_wheelEncoder.omega_l = 0.0f;
78  }
79  if(speedR)
80  {
81  g_wheelEncoder.omega_r = g_flashCfg->encoderTickToWheelRad / (0.00002f * (float)speedR);
82  }
83  else
84  {
85  g_wheelEncoder.omega_r = 0.0f;
86  }
87 
88  // Encoder Wrap count (currently counting revolutions)
89 // g_wheelEncoder.wrap_count_l = g_wheelEncoder.theta_l / (2*PI);
90 // g_wheelEncoder.wrap_count_r = g_wheelEncoder.theta_r / (2*PI);
91 
92  n = is_comm_data(&comm, DID_WHEEL_ENCODER, 0, sizeof(wheel_encoder_t), (void*)&(g_wheelEncoder));
94 
95  // Update history
96  wheelEncoderLast = g_wheelEncoder;
97  }
98 
99 #endif
100 }
uint32_t bits
Definition: data_sets.h:3032
double time_seclf(void)
Definition: d_time.c:143
void init_control(void)
Definition: control_law.cpp:20
void quadEncInit(void)
Initialize quadrature encoder driver.
Definition: d_quadEnc.c:150
int comWrite(int serialNum, const unsigned char *buf, int size, uint32_t ledPin)
void quadEncReadSpeedAll(uint32_t *speed0, uint32_t *speed1)
Definition: d_quadEnc.c:198
float omega_r
Definition: data_sets.h:1790
float theta_l
Definition: data_sets.h:1781
void quadEncReadPositionAll(int *pos0, bool *dir0, int *pos1, bool *dir1)
Reads the current position of the encoders.
Definition: d_quadEnc.c:181
#define LED_INS_TXD_PIN
Definition: user_board.h:280
wheel_encoder_t g_wheelEncoder
Definition: globals.c:20
void velocity_control(is_comm_instance_t &comm)
Definition: control_law.cpp:31
float encoderTickToWheelRad
Definition: data_sets.h:3050
double timeOfWeek
Definition: data_sets.h:417
is_comm_buffer_t buf
Definition: ISComm.h:488
float theta_r
Definition: data_sets.h:1784
uint8_t * start
Definition: ISComm.h:447
uint32_t g_comm_time_ms
Definition: globals.c:32
evb_flash_cfg_t * g_flashCfg
Definition: globals.c:22
#define DID_WHEEL_ENCODER
Definition: data_sets.h:105
int is_comm_data(is_comm_instance_t *instance, uint32_t dataId, uint32_t offset, uint32_t size, void *data)
Definition: ISComm.c:639
Autogenerated API include file for the Atmel Software Framework (ASF)
float omega_l
Definition: data_sets.h:1787


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04