1+ /*
2+ This file is part of the Arduino_GroveI2C_Ultrasonic library.
3+ Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+ This library is free software; you can redistribute it and/or
6+ modify it under the terms of the GNU Lesser General Public
7+ License as published by the Free Software Foundation; either
8+ version 2.1 of the License, or (at your option) any later version.
9+
10+ This library is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+ Lesser General Public License for more details.
14+
15+ You should have received a copy of the GNU Lesser General Public
16+ License along with this library; if not, write to the Free Software
17+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+ */
19+
20+ #include " Arduino_Robot_Firmware.h"
21+ #include " sensor_line.h"
22+ #include " sensor_tof_matrix.h"
23+ #include " ucPack.h"
24+
25+ Arduino_Robot_Firmware robot;
26+ SensorLine line (EXT_A2,EXT_A1,EXT_A0);
27+ SensorTofMatrix tof (robot.wire, EXT_GPIO3, EXT_GPIO2);
28+
29+
30+ ucPack packeter (200 );
31+
32+ uint8_t code;
33+ uint8_t msg_size;
34+
35+ unsigned long tmotor=0 ;
36+ unsigned long tsend=0 ;
37+ unsigned long tsensor=0 ;
38+
39+
40+ float left, right;
41+ uint8_t leds;
42+
43+ uint8_t sensor_id = 0 ;
44+
45+
46+ uint8_t pid;
47+ float kp, ki, kd;
48+
49+
50+
51+ void setup (){
52+ Serial.begin (115200 );
53+ robot.begin ();
54+ robot.setLedBuiltin (HIGH);
55+ line.begin ();
56+ tof.begin ();
57+
58+ uint8_t version[3 ];
59+ robot.getVersion (version[0 ], version[1 ], version[2 ]);
60+ msg_size = packeter.packetC3B (0x7E , version[0 ], version[1 ], version[2 ]);
61+ robot.serial ->write (packeter.msg ,msg_size);
62+
63+ robot.setLedBuiltin (LOW);
64+
65+
66+ code=0 ;
67+ tmotor=millis ();
68+ tsend=millis ();
69+ tsensor=millis ();
70+ }
71+
72+ void loop (){
73+ while (robot.serial ->available () > 0 ) {
74+ packeter.buffer .push (robot.serial ->read ());
75+ }
76+ if (packeter.checkPayload ()) {
77+ code = packeter.payloadTop ();
78+ switch (code){
79+ case ' J' :
80+ packeter.unpacketC2F (code,left,right);
81+ robot.setRpm (left, right);
82+ break ;
83+ /*
84+ case 'S':
85+ robot.setRpm(0,0);
86+ break;
87+ */
88+ case ' L' :
89+ packeter.unpacketC1B (code,leds);
90+ robot.setAllLeds (leds);
91+ break ;
92+
93+ case ' P' :
94+ packeter.unpacketC1B3F (code,pid,kp,ki,kd);
95+ if (pid==' L' ){
96+ robot.setKPidLeft (kp,ki,kd);
97+ }
98+ if (pid==' R' ){
99+ robot.setKPidRight (kp,ki,kd);
100+ }
101+ break ;
102+ }
103+ }
104+
105+ if (millis ()-tsensor>10 ){
106+ tsensor=millis ();
107+ switch (sensor_id){
108+ case 0 :
109+ line.update ();
110+ msg_size = packeter.packetC3I (' l' , line.getLeft (), line.getCenter (), line.getRight ());
111+ robot.serial ->write (packeter.msg ,msg_size);
112+ break ;
113+ case 1 :
114+ robot.updateTouch ();
115+ msg_size = packeter.packetC1B (' t' , robot.getTouchKeys ());
116+ robot.serial ->write (packeter.msg ,msg_size);
117+ break ;
118+ case 2 :
119+ robot.updateAPDS ();
120+ msg_size = packeter.packetC3I (' c' , robot.getRed (), robot.getGreen (), robot.getBlue ());
121+ robot.serial ->write (packeter.msg ,msg_size);
122+ break ;
123+ case 3 :
124+ tof.update ();
125+ msg_size = packeter.packetC7I (' f' , tof.getLeft (), tof.getCenterLeft (), tof.getCenter (), tof.getCenterRight (), tof.getRight (), tof.get_min_range_top_mm (), tof.get_max_range_bottom_mm ());
126+ robot.serial ->write (packeter.msg ,msg_size);
127+ break ;
128+ case 4 :
129+ msg_size = packeter.packetC3F (' q' , robot.getRoll (), robot.getPitch (), robot.getYaw ());
130+ robot.serial ->write (packeter.msg ,msg_size);
131+ break ;
132+ }
133+ sensor_id++;
134+ if (sensor_id>4 ){
135+ sensor_id=0 ;
136+ }
137+ }
138+
139+ if (millis ()-tmotor>10 ){
140+ tmotor=millis ();
141+ robot.updateMotors ();
142+ robot.updateImu ();
143+ msg_size = packeter.packetC2F (' j' , robot.getRpmLeft (),robot.getRpmRight ());
144+ robot.serial ->write (packeter.msg ,msg_size);
145+ msg_size = packeter.packetC6F (' i' , robot.getAccelerationX (), robot.getAccelerationY (), robot.getAccelerationZ (), robot.getAngularVelocityX (), robot.getAngularVelocityY (), robot.getAngularVelocityZ ());
146+ robot.serial ->write (packeter.msg ,msg_size);
147+ }
148+ }
0 commit comments