Skip to content

Commit af41906

Browse files
committed
Changed Motor Controls to load from file #10
1 parent 1c3b5bd commit af41906

File tree

7 files changed

+83
-28
lines changed

7 files changed

+83
-28
lines changed

scarborough/src/Arduino_Communications/arduino_i2c.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* Author: sdcr
66
*/
77
#include "arduino_i2c.h"
8-
#include "arduino_comm.h"
98

109
I2Cdev i2cdev;
1110

1211
ArdI2C i2c;
13-
ArdComm ardcomm;
1412
Handler handler;
1513

1614
double imu_data[3];
@@ -45,18 +43,19 @@ int main(int argc, char **argv){
4543
ros::NodeHandle n;
4644

4745
ros::Subscriber imu;
48-
ros::Publisher ard_pub = n.advertise<scarborough::Motor_Speed>(handler.MOTORS, 100);
4946
ros::Publisher depth_pub = n.advertise<scarborough::Depth>(handler.DEPTH_SENSOR, 100);
5047
ros::Publisher kill_pub = n.advertise<scarborough::Kill_Switch>(handler.KILL, 10);
5148

5249
ros::Subscriber sub;
5350

54-
i2c.init(); // does nothing.
51+
i2c.init();
5552

5653
imu = IMU.subscribe(handler.IMU, 200, getdata);
5754
sub = n.subscribe(handler.DESIRED, 200, getdata_DESIRED);
5855
int kill;
5956
double depth;
57+
scarborough::Kill_Switch kill_switch;
58+
scarborough::Depth depth_sensor;
6059

6160

6261
while(ros::ok()){
@@ -67,24 +66,24 @@ int main(int argc, char **argv){
6766
kill = i2c.read_kill();
6867
depth = i2c.read_depth();
6968

70-
ardcomm.depth.depth = depth;
69+
depth_sensor.depth = depth;
7170

7271
if(kill == 1){
73-
ardcomm.kill_switch.killed = false;
72+
kill_switch.killed = false;
7473
}
7574
else{
76-
ardcomm.kill_switch.killed = true;
75+
kill_switch.killed = true;
7776
}
7877

7978

8079
//publish motor data to the ARD_I2C
8180
//ard_pub.publish(ardcomm.motor);
82-
depth_pub.publish(ardcomm.depth);
83-
kill_pub.publish(ardcomm.kill_switch);
81+
depth_pub.publish(depth_sensor);
82+
kill_pub.publish(kill_switch);
8483

8584
//cout << ardcomm.motor << endl;
86-
cout << ardcomm.depth << endl;
87-
cout << ardcomm.kill_switch << endl;
85+
cout << depth_sensor << endl;
86+
cout << kill_switch << endl;
8887

8988

9089

@@ -107,8 +106,7 @@ ArdI2C::ArdI2C(){
107106
//use this if ever needed... It used to do something but is now a ghost of algorithms past
108107
void ArdI2C::init(){
109108

110-
//write the pid values to the teensy
111-
//pid_Set();
109+
restart_arduino();
112110

113111

114112
}
@@ -197,9 +195,6 @@ float ArdI2C::read_depth(){
197195

198196
combined = d[0] | (d[1] << 8);
199197

200-
201-
cout << combined << endl;
202-
203198
//we multiplied it by 1000 on the arduino side
204199
float result = (float)(combined) / 1000.00;
205200

@@ -242,6 +237,17 @@ void ArdI2C::update_desired(scarborough::Desired_Directions _desired){
242237

243238
}
244239

240+
void ArdI2C::restart_arduino(){
241+
//write 1 to the arduino at the 15 address to restart it
242+
i2cdev.writeByte(0x04, 15, 1);
243+
244+
}
245+
void ArdI2C::restart_depth_sensor(){
246+
//write 1 to the arduino at the 15 address to reinitialize the depth sensor
247+
i2cdev.writeByte(0x04, 16, 1);
248+
249+
}
250+
245251
//reg 15 - 38
246252
void ArdI2C::pid_Set(){
247253
cout<<"Initializing pid values" <<endl;

scarborough/src/Arduino_Communications/arduino_i2c.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "scarborough/YPR.h"
3232
#include "scarborough/Desired_Directions.h"
3333
#include "../HandlerNames/HANDLER_NAMES.h"
34+
#include "scarborough/Kill_Switch.h"
35+
#include "scarborough/Depth.h"
3436
#include <math.h>
3537
#include <stdint.h>
3638
#include <inttypes.h>
@@ -42,6 +44,8 @@ class ArdI2C{
4244
public:
4345
ArdI2C();
4446
void get_accel_info(); // This will query the correct ROS stream to get the IMU data
47+
void restart_arduino();
48+
void restart_depth_sensor();
4549
string ardRead();
4650
int read_kill();
4751
float read_depth();

scarborough/src/Motor_Controls/Helm.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ Helm::Helm(){
77
ROLL_CONST = 1;
88
DEPTH_CONST = 1;
99

10-
//Here is the actual PID value for the robot
11-
double pid[3] = {
12-
65,
13-
0,
14-
0
15-
};
10+
//this will initialize the PID value
11+
load_yaml();
1612
//this gives the PID the P, I and D values then limits their output with a min and a max
1713
yaw_pid.initPid(pid[0], pid[1], pid[2], 1200, 1800);
1814
pitch_pid.initPid(pid[0], pid[1], pid[2], 1200, 1800);
@@ -23,7 +19,48 @@ Helm::Helm(){
2319

2420
void Helm::init(){
2521
//initialize the time
26-
last_time = ros::Time::now();
22+
last_time = ros::Time::now();
23+
24+
25+
}
26+
27+
void Helm::load_yaml(){
28+
ifstream config("/home/ros/PID_config.txt");
29+
30+
double P = 0.00;
31+
double I = 0.00;
32+
double D = 0.00;
33+
34+
35+
for( string line; getline( config, line ); )
36+
{
37+
cout << line << endl;
38+
switch(line[0]){
39+
40+
case 'P':
41+
P = atof( line.substr(1, line.length()).c_str() );
42+
break;
43+
case 'I':
44+
I = atof( line.substr(1, line.length()).c_str() );
45+
break;
46+
case 'D':
47+
D = atof( line.substr(1, line.length()).c_str() );
48+
break;
49+
default:
50+
break;
51+
52+
53+
}
54+
}
55+
56+
cout << "P: " << P << endl;
57+
cout << "I: " << I << endl;
58+
cout << "D: " << D << endl;
59+
60+
pid[0] = P;
61+
pid[1] = I;
62+
pid[2] = D;
63+
2764
}
2865

2966

scarborough/src/Motor_Controls/Helm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include <stdlib.h>
1313
#include <algorithm>
1414
#include <string>
15-
15+
#include <iostream>
16+
#include <string>
17+
#include <fstream>
1618

1719
using namespace std;
1820

@@ -36,11 +38,13 @@ class Helm{
3638
void update_depth(scarborough::Depth _depth);
3739
void computron();
3840
void reset();
41+
void load_yaml();
3942

4043
//returns the smallest absolute value angle equivalent to the given angle
4144
static double normalizeAngle(const double& angle); //degrees
4245

4346
double YAW_CONST, PITCH_CONST, ROLL_CONST, DEPTH_CONST;
47+
double pid[3];
4448

4549

4650

scarborough/src/Motor_Controls/I2Cdev.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16
172172
*/
173173
int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) {
174174
int8_t count = 0;
175-
int fd = open("/dev/i2c-0",O_RDWR);
175+
int fd = open("/dev/i2c-1",O_RDWR);
176176

177177
if (fd < 0) {
178178
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
@@ -344,7 +344,7 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_
344344
return(FALSE);
345345
}
346346

347-
fd = open("/dev/i2c-0", O_RDWR);
347+
fd = open("/dev/i2c-1", O_RDWR);
348348
if (fd < 0) {
349349
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
350350
return(FALSE);
@@ -391,7 +391,7 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16
391391
return(FALSE);
392392
}
393393

394-
fd = open("/dev/i2c-0", O_RDWR);
394+
fd = open("/dev/i2c-1", O_RDWR);
395395
if (fd < 0) {
396396
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
397397
return(FALSE);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PID:
2+
P:60
3+
I:0
4+
D:0
5+

scarborough/src/Motor_Controls/motors_scarborough.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ int main(int argc, char **argv){
8585

8686
//if the kill switch is off do not send motor signals
8787
if(!killed){
88-
cout << "Get Data for PID" << endl;
8988
if(restart){
9089
motors.kill_motors();
9190
helm.reset();

0 commit comments

Comments
 (0)