<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[RemoteXY community — Балансирующий робот]]></title>
		<link>https://forum.remotexy.com/viewtopic.php?id=386</link>
		<atom:link href="https://forum.remotexy.com/extern.php?action=feed&amp;tid=386&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Балансирующий робот.]]></description>
		<lastBuildDate>Thu, 08 Feb 2018 11:32:41 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Балансирующий робот]]></title>
			<link>https://forum.remotexy.com/viewtopic.php?pid=1315#p1315</link>
			<description><![CDATA[<p>хотел собрать балансирующего робота и привязать его управлению через блютуз, при проверке кода выдает ошибку, подскажите как это исправить?</p><p>ошибка при компиляции и код программы ниже</p><p>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\<br />C:\Users\Oleg_Y500\Documents\Arduino\sketch_feb08c\sketch_feb08c.ino: In function &#039;void setup()&#039;:</p><p>sketch_feb08c:51: error: &#039;i2cWrite&#039; was not declared in this scope</p><p>&nbsp; &nbsp;while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^</p><p>sketch_feb08c:52: error: &#039;i2cWrite&#039; was not declared in this scope</p><p>&nbsp; &nbsp;while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^</p><p>sketch_feb08c:54: error: &#039;i2cRead&#039; was not declared in this scope</p><p>&nbsp; &nbsp;while (i2cRead(0x75, i2cData, 1));</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</p><p>sketch_feb08c:65: error: &#039;i2cRead&#039; was not declared in this scope</p><p>&nbsp; &nbsp;while (i2cRead(0x3B, i2cData, 6));</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</p><p>C:\Users\Oleg_Y500\Documents\Arduino\sketch_feb08c\sketch_feb08c.ino: In function &#039;void loop()&#039;:</p><p>sketch_feb08c:91: error: &#039;i2cRead&#039; was not declared in this scope</p><p>&nbsp; &nbsp; &nbsp;while (i2cRead(0x3B, i2cData, 14));</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^</p><p>exit status 1<br />&#039;i2cWrite&#039; was not declared in this scope</p><p>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\</p><p>Пример кода</p><p>#include &lt;Wire.h&gt;<br />#include &quot;Kalman.h&quot; <br />#define RESTRICT_PITCH <br />Kalman kalmanX;<br />Kalman kalmanY;<br />double accX, accY, accZ;<br />double gyroX, gyroY, gyroZ;<br />int16_t tempRaw;</p><p>double gyroXangle, gyroYangle; // Gyroscope angle<br />double compAngleX, compAngleY; // Complementary filter angle <br />double kalAngleX, kalAngleY; // Angle after Kalman filter<br />double corrected_x, corrected_y; // Corrected with offset</p><p>uint32_t timer;<br />uint8_t i2cData[14]; // Buffer for I2C data</p><p>char a;<br />double m = 0.7;<br />double m1 = -0.7;<br />int d = 0;<br />int c = 0;<br />char p;<br />int in1_motor_left = 8;<br />int in2_motor_left = 7;<br />int in3_motor_right = 3;<br />int in4_motor_right = 4;<br />int pwm_on = 5; // ms ON<br />int pwm_off = 5; // ms OFF<br />//------------------------------------------------------------------------------<br />void setup() {<br />&nbsp; // Define outputs <br />&nbsp; pinMode(in1_motor_left, OUTPUT);<br />&nbsp; pinMode(in2_motor_left, OUTPUT);<br />&nbsp; pinMode(in3_motor_right, OUTPUT);<br />&nbsp; pinMode(in4_motor_right, OUTPUT);<br />&nbsp; // Start serial console<br />&nbsp; Serial.begin(115200);<br />&nbsp; //BT.begin(9600);<br />&nbsp; delay(50);<br />&nbsp; // Initiate the Wire library and join the I2C bus as a master or slave<br />&nbsp; Wire.begin();</p><p>&nbsp; TWBR = ((F_CPU / 400000L) - 16) / 2; // Set I2C frequency to 400kHz</p><p>&nbsp; i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz<br />&nbsp; i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling<br />&nbsp; i2cData[2] = 0x00; // Set Gyro Full Scale Range to 250deg/s<br />&nbsp; i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to 2g</p><p>&nbsp; while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once<br />&nbsp; while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode</p><p>&nbsp; while (i2cRead(0x75, i2cData, 1));<br />&nbsp; if (i2cData[0] != 0x68) { // Read &quot;WHO_AM_I&quot; register<br />&nbsp; &nbsp; Serial.print(F(&quot;Error reading sensor&quot;));<br />&nbsp; &nbsp; while (1);<br />&nbsp; }<br />&nbsp; delay(100); // Wait for sensor to stabilize</p><p>/** <br />* Set kalman and gyro starting angle<br />*<br />*/<br />&nbsp; while (i2cRead(0x3B, i2cData, 6));<br />&nbsp; accX = (i2cData[0] &lt;&lt; 8) | i2cData[1];<br />&nbsp; accY = (i2cData[2] &lt;&lt; 8) | i2cData[3];<br />&nbsp; accZ = (i2cData[4] &lt;&lt; 8) | i2cData[5];</p><p>&nbsp; // atan2 outputs the value of - to&nbsp; (radians) - see <a target="_blank" href="http://en.wikipedia.org/wiki/Atan2">http://en.wikipedia.org/wiki/Atan2</a><br />&nbsp; // It is then converted from radians to degrees<br />&nbsp; #ifdef RESTRICT_PITCH<br />&nbsp; &nbsp; double roll&nbsp; = atan2(accY, accZ) * RAD_TO_DEG;<br />&nbsp; &nbsp; double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;<br />&nbsp; #else<br />&nbsp; &nbsp; double roll&nbsp; = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;<br />&nbsp; &nbsp; double pitch = atan2(-accX, accZ) * RAD_TO_DEG;<br />&nbsp; #endif</p><p>&nbsp; kalmanX.setAngle(roll);<br />&nbsp; kalmanY.setAngle(pitch);<br />&nbsp; gyroXangle = roll;<br />&nbsp; gyroYangle = pitch;<br />&nbsp; compAngleX = roll;<br />&nbsp; compAngleY = pitch;<br />&nbsp; timer = micros();<br />}</p><p>//------------------------------------------------------------------------------<br />void loop() {<br />&nbsp; &nbsp; while (i2cRead(0x3B, i2cData, 14));<br />&nbsp; &nbsp; accX = ((i2cData[0] &lt;&lt; 8) | i2cData[1]);<br />&nbsp; &nbsp; accY = ((i2cData[2] &lt;&lt; 8) | i2cData[3]);<br />&nbsp; &nbsp; accZ = ((i2cData[4] &lt;&lt; 8) | i2cData[5]);<br />&nbsp; &nbsp; tempRaw = (i2cData[6] &lt;&lt; 8) | i2cData[7];<br />&nbsp; &nbsp; gyroX = (i2cData[8] &lt;&lt; 8) | i2cData[9];<br />&nbsp; &nbsp; gyroY = (i2cData[10] &lt;&lt; 8) | i2cData[11];<br />&nbsp; &nbsp; gyroZ = (i2cData[12] &lt;&lt; 8) | i2cData[13];<br />&nbsp; &nbsp; // Calculate delta time<br />&nbsp; &nbsp; double dt = (double)(micros() - timer) / 1000000; <br />&nbsp; &nbsp; timer = micros();</p><br /><p>&nbsp; #ifdef RESTRICT_PITCH<br />&nbsp; &nbsp; double roll&nbsp; = atan2(accY, accZ) * RAD_TO_DEG;<br />&nbsp; &nbsp; double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;<br />&nbsp; #else <br />&nbsp; &nbsp; double roll&nbsp; = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;<br />&nbsp; &nbsp; double pitch = atan2(-accX, accZ) * RAD_TO_DEG;<br />&nbsp; #endif</p><p>&nbsp; double gyroXrate = gyroX / 131.0; // Convert to deg/s<br />&nbsp; double gyroYrate = gyroY / 131.0; // Convert to deg/s</p><p>&nbsp; #ifdef RESTRICT_PITCH<br />&nbsp; &nbsp; // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees<br />&nbsp; &nbsp; if ((roll &lt; -90 &amp;&amp; kalAngleX &gt; 90) || (roll &gt; 90 &amp;&amp; kalAngleX &lt; -90)) {<br />&nbsp; &nbsp; &nbsp; kalmanX.setAngle(roll);<br />&nbsp; &nbsp; &nbsp; compAngleX = roll;<br />&nbsp; &nbsp; &nbsp; kalAngleX = roll;<br />&nbsp; &nbsp; &nbsp; gyroXangle = roll;<br />&nbsp; &nbsp; } else<br />&nbsp; &nbsp; &nbsp; kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter</p><p>&nbsp; &nbsp; if (abs(kalAngleX) &gt; 90)<br />&nbsp; &nbsp; &nbsp; gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading<br />&nbsp; &nbsp; kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);<br />&nbsp; #else<br />&nbsp; &nbsp; // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees<br />&nbsp; &nbsp; if ((pitch &lt; -90 &amp;&amp; kalAngleY &gt; 90) || (pitch &gt; 90 &amp;&amp; kalAngleY &lt; -90)) {<br />&nbsp; &nbsp; &nbsp; kalmanY.setAngle(pitch);<br />&nbsp; &nbsp; &nbsp; compAngleY = pitch;<br />&nbsp; &nbsp; &nbsp; kalAngleY = pitch;<br />&nbsp; &nbsp; &nbsp; gyroYangle = pitch;<br />&nbsp; &nbsp; } else<br />&nbsp; &nbsp; &nbsp; kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter</p><p>&nbsp; &nbsp; if (abs(kalAngleY) &gt; 90)<br />&nbsp; &nbsp; &nbsp; gyroXrate = -gyroXrate; // Invert rate, so it fits the restriced accelerometer reading<br />&nbsp; &nbsp; kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter<br />&nbsp; #endif</p><p>&nbsp; &nbsp; gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter<br />&nbsp; &nbsp; gyroYangle += gyroYrate * dt;<br />&nbsp; &nbsp; compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter<br />&nbsp; &nbsp; compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;</p><p>&nbsp; &nbsp; // Reset the gyro angle when it has drifted too much<br />&nbsp; &nbsp; if (gyroXangle &lt; -180 || gyroXangle &gt; 180)<br />&nbsp; &nbsp; &nbsp; gyroXangle = kalAngleX;<br />&nbsp; &nbsp; if (gyroYangle &lt; -180 || gyroYangle &gt; 180)<br />&nbsp; &nbsp; &nbsp; gyroYangle = kalAngleY;<br />&nbsp; &nbsp; delay(2);<br />&nbsp; &nbsp; Serial.println();<br />&nbsp; &nbsp; // Corrected angles with offset<br />&nbsp; &nbsp; corrected_x=kalAngleX-171,746;<br />&nbsp; &nbsp; corrected_y=kalAngleY-81,80;<br />&nbsp; corrected_y = corrected_y+84;<br />&nbsp; Serial.print(corrected_y);<br />&nbsp; pwm_adjust(corrected_y);<br />&nbsp; if(corrected_y&gt;=m &amp;&amp; corrected_y&lt;20){<br />&nbsp; &nbsp;if(c&gt;6){<br />&nbsp; &nbsp; m-=0.2;<br />&nbsp; &nbsp; m1-=-0.2;<br />&nbsp; &nbsp; c=0;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; backward();<br />&nbsp; &nbsp;}<br />&nbsp; &nbsp;else if(corrected_y&gt;=-20 &amp;&amp; corrected_y&lt;=m1){<br />&nbsp; &nbsp; Serial.print(&quot;&nbsp; &quot;);<br />&nbsp; &nbsp; if(d&gt;6){<br />&nbsp; &nbsp; m+=0.2;<br />&nbsp; &nbsp; m1+=0.2;<br />&nbsp; &nbsp; d=0;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; forward();&nbsp; &nbsp;<br />&nbsp; }else{<br />&nbsp; &nbsp; stop();<br />&nbsp; &nbsp; m=0.7;<br />&nbsp; &nbsp; m1=-0.7;<br />&nbsp; &nbsp; pwm_on = 0;<br />&nbsp; &nbsp; pwm_off = 0;<br />&nbsp; }</p><p>}</p><p>void forward(){<br />&nbsp; d++;<br />&nbsp; //Serial.print(d);<br />&nbsp; digitalWrite(in3_motor_right, LOW);<br />&nbsp; digitalWrite(in4_motor_right, HIGH);<br />&nbsp; digitalWrite(in1_motor_left, HIGH);<br />&nbsp; digitalWrite(in2_motor_left, LOW);<br />&nbsp; delay(pwm_on);</p><p>&nbsp; digitalWrite(in3_motor_right, LOW);<br />&nbsp; digitalWrite(in4_motor_right, LOW);<br />&nbsp; digitalWrite(in1_motor_left, LOW);<br />&nbsp; digitalWrite(in2_motor_left, LOW);<br />&nbsp; delay(pwm_off);<br />}</p><p>void backward(){<br />&nbsp; c++;<br />&nbsp; digitalWrite(in3_motor_right, HIGH);<br />&nbsp; digitalWrite(in4_motor_right,LOW);<br />&nbsp; digitalWrite(in1_motor_left, LOW);<br />&nbsp; digitalWrite(in2_motor_left, HIGH);<br />&nbsp; delay(pwm_on);</p><p>&nbsp; digitalWrite(in3_motor_right, LOW);<br />&nbsp; digitalWrite(in4_motor_right, LOW);<br />&nbsp; digitalWrite(in1_motor_left, LOW);<br />&nbsp; digitalWrite(in2_motor_left, LOW);<br />&nbsp; delay(pwm_off);<br />}</p><p>void stop(){<br />&nbsp; digitalWrite(in1_motor_left, LOW);<br />&nbsp; digitalWrite(in2_motor_left, LOW);<br />&nbsp; digitalWrite(in3_motor_right, LOW);<br />&nbsp; digitalWrite(in4_motor_right, LOW);<br />&nbsp; delay(pwm_on);</p><p>&nbsp; digitalWrite(in1_motor_left, LOW);<br />&nbsp; digitalWrite(in2_motor_left, LOW);<br />&nbsp; digitalWrite(in3_motor_right, LOW);<br />&nbsp; digitalWrite(in4_motor_right, LOW);<br />&nbsp; delay(pwm_off);<br />}<br />void pwm_adjust(int value_y){<br />&nbsp; <br />&nbsp; if(value_y &gt;=-1 &amp;&amp; value_y &lt;=1 ){<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; int k = (value_y*value_y);<br />&nbsp; &nbsp; Serial.print(k);<br />&nbsp; &nbsp; pwm_on = 5; // ms ON<br />&nbsp; &nbsp; pwm_off = 3; // ms OFF&nbsp; <br />&nbsp; }<br /> else if((value_y&gt;=-3 &amp;&amp; value_y&lt;-1)||(value_y&gt;1 &amp;&amp; value_y&lt;=3) ){<br />&nbsp; &nbsp; pwm_on = 50;<br />&nbsp; &nbsp; pwm_off = 5;<br />&nbsp; }<br />&nbsp; else if(value_y &gt;5 || value_y &lt;=-5 ){<br />&nbsp; &nbsp; Serial.print(&quot;**&quot;);<br />&nbsp; &nbsp; pwm_on = 120; // ms ON<br />&nbsp; &nbsp; pwm_off = 3; // ms OFF&nbsp; <br />&nbsp; }<br />&nbsp; else<br />&nbsp; &nbsp; stop();<br />&nbsp; <br />&nbsp; }</p>]]></description>
			<author><![CDATA[null@example.com (olegeglit)]]></author>
			<pubDate>Thu, 08 Feb 2018 11:32:41 +0000</pubDate>
			<guid>https://forum.remotexy.com/viewtopic.php?pid=1315#p1315</guid>
		</item>
	</channel>
</rss>
