scoutos / scout / painter / src / paint-i2c.cpp @ 32c4f513
History | View | Annotate | Download (4.36 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2013 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
*/
|
25 |
|
26 |
/**
|
27 |
* @file paint_i2c.cpp
|
28 |
* @brief Contains i2c communication for the paintboard.
|
29 |
*
|
30 |
* @author Colony Project, CMU Robotics Club
|
31 |
* @author Maung Aung (mza)
|
32 |
* @author Tom Mullins (tmullins)
|
33 |
*
|
34 |
*/
|
35 |
|
36 |
#include <errno.h> |
37 |
#include <stdio.h> |
38 |
#include <stdlib.h> |
39 |
#include <unistd.h> |
40 |
#include <sys/types.h> |
41 |
#include <sys/stat.h> |
42 |
#include <sys/ioctl.h> |
43 |
#include <fcntl.h> |
44 |
#include <linux/i2c-dev.h> |
45 |
#include "paint-i2c.h" |
46 |
|
47 |
#define TRACKING_ID 0x43 |
48 |
#define SERIAL_NUMBER 0x12 |
49 |
|
50 |
// indicies for paintboard internal data
|
51 |
#define PAINT_TRACKING_ID 0 // ro |
52 |
#define PAINT_SERIAL_NUMBER 1 // ro |
53 |
#define PAINT_MOTOR_A 2 // rw |
54 |
#define PAINT_MOTOR_B 3 // rw |
55 |
#define PAINT_SERVO_A 4 // rw |
56 |
#define PAINT_SERVO_B 5 // rw |
57 |
#define PAINT_12V_1 6 // rw |
58 |
#define PAINT_12V_2 7 // rw |
59 |
#define PAINT_12V_3 8 // rw |
60 |
#define PAINT_12V_4 9 // rw |
61 |
#define PAINT_INPUT_1 10 // ro |
62 |
#define PAINT_INPUT_2 11 // ro |
63 |
#define PAINT_INPUT_3 12 // ro |
64 |
|
65 |
#define PAINT_DATA_LEN 13 |
66 |
|
67 |
int fd;
|
68 |
|
69 |
/*
|
70 |
* This function opens communication to I2C.
|
71 |
*/
|
72 |
void i2c_start(void) { |
73 |
/* TODO error check */
|
74 |
fd = open("/dev/i2c-3", O_RDWR);
|
75 |
if (fd < 0) { |
76 |
perror("/dev/i2c-3");
|
77 |
exit(1);
|
78 |
} |
79 |
if (ioctl(fd, I2C_SLAVE, TRACKING_ID)) {
|
80 |
perror("ioctl");
|
81 |
exit(1);
|
82 |
} |
83 |
} |
84 |
|
85 |
/*
|
86 |
* This function ends communication with I2C.
|
87 |
*/
|
88 |
void i2c_stop(void) { |
89 |
if (fd) {
|
90 |
close(fd); |
91 |
} |
92 |
} |
93 |
|
94 |
/*
|
95 |
* This function writes a value to a given destination using I2C.
|
96 |
*/
|
97 |
void i2c_write(int dest, int val) { |
98 |
char buf[2]; |
99 |
buf[0] = (char) dest; |
100 |
buf[1] = (char) val; |
101 |
|
102 |
if (write(fd, buf, 2) < 2) { |
103 |
fprintf(stderr, "Warning: i2c_write failed\n");
|
104 |
} |
105 |
} |
106 |
|
107 |
/*
|
108 |
* This function reads a value from the given address using I2C
|
109 |
*/
|
110 |
char i2c_read(int src) { |
111 |
char buf;
|
112 |
|
113 |
buf = src; |
114 |
if (write(fd, &buf, 1) < 1) { |
115 |
fprintf(stderr, "Warning: write in i2c_read failed\n");
|
116 |
} else if (read(fd, &buf, 1) < 1) { |
117 |
fprintf(stderr, "Warning: read in i2c_read failed\n");
|
118 |
} |
119 |
return buf;
|
120 |
} |
121 |
|
122 |
/*
|
123 |
* This function takes in an int representing the motor to be modified and an
|
124 |
* int representing the value to which to set the motor.
|
125 |
*/
|
126 |
void set_motor(int motor, int val) { |
127 |
if (motor == 0) { |
128 |
i2c_write(PAINT_MOTOR_A, val); |
129 |
} else {
|
130 |
i2c_write(PAINT_MOTOR_B, val); |
131 |
} |
132 |
} |
133 |
|
134 |
/*
|
135 |
* This function takes in an int representing the servo to be modified and an
|
136 |
* int representing the value to which to set the servo.
|
137 |
*/
|
138 |
void set_servo(int servo, int val) { |
139 |
if (servo == 0) { |
140 |
i2c_write(PAINT_SERVO_A, val); |
141 |
} else {
|
142 |
i2c_write(PAINT_SERVO_B, val); |
143 |
} |
144 |
} |
145 |
|
146 |
/*
|
147 |
* This function takes in an int representing the solenoid to be modified and
|
148 |
* an int representing the value to which to set the solenoid.
|
149 |
*/
|
150 |
void set_solenoid(int solenoid, int val) { |
151 |
if (solenoid == 0) { |
152 |
i2c_write(PAINT_12V_1, val); |
153 |
} else if (solenoid == 1) { |
154 |
i2c_write(PAINT_12V_2, val); |
155 |
} else if (solenoid == 2) { |
156 |
i2c_write(PAINT_12V_3, val); |
157 |
} else {
|
158 |
i2c_write(PAINT_12V_4, val); |
159 |
} |
160 |
} |
161 |
|
162 |
/*
|
163 |
* This funciton takes an int representing which of the spare inputs to read.
|
164 |
*/
|
165 |
int get_input(int input) { |
166 |
switch (input) {
|
167 |
case 0: return i2c_read(PAINT_INPUT_1); |
168 |
case 1: return i2c_read(PAINT_INPUT_2); |
169 |
case 2: return i2c_read(PAINT_INPUT_3); |
170 |
default: return 0; |
171 |
} |
172 |
} |