<xml xmlns="https://developers.google.com/blockly/xml">
  <variables>
    <variable id="QB$Sn5l|I2:-d992~yO[">Ax</variable>
    <variable id="hFT`T6F(Qq$Y*Egy^K31">Ay</variable>
    <variable id=",~DkqEJr=3#Az#P/`na3">Az</variable>
  </variables>
  <block type="controls_repeat_ext" id=",C%r5nZGnQ|4+T6e/^?e" x="21" y="-214">
    <value name="TIMES">
      <shadow type="math_number" id="F{ylvzrl)kPMN:`{4=:!">
        <field name="NUM">10000</field>
      </shadow>
    </value>
    <statement name="DO">
      <block type="variables_set" id="lhF%$qCn8qh@:pEi51@F">
        <field name="VAR" id="QB$Sn5l|I2:-d992~yO[">Ax</field>
        <value name="VALUE">
          <block type="math_arithmetic" id="Qj|iPm?xT!2zM~TZS4t=">
            <field name="OP">MULTIPLY</field>
            <comment pinned="false" h="80" w="160">X is from -1 to 1
        Add 1 to make it 0-2. Multiple by 90 to get 0-180 angle
                
                
            </comment>
            <value name="A">
              <shadow type="math_number" id="2?Cstc_}51C_p3|C+;,+">
                <field name="NUM">90</field>
              </shadow>
            </value>
            <value name="B">
              <shadow type="math_number" id="q+Hv0s;T~nshse{y=tec">
                <field name="NUM">1</field>
              </shadow>
              <block type="math_arithmetic" id="`7*[QdTfgI?:9*Jw$IOM">
                <field name="OP">ADD</field>
                <value name="A">
                  <shadow type="math_number" id="(LMgK)uR2Q~*5Lnzm|Vr">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <value name="B">
                  <shadow type="math_number" id="i)1^xR;o3nS|7;]+rU??">
                    <field name="NUM">1</field>
                  </shadow>
                  <block type="get_phone_rotation" id="U-6EU:)dZ}{DV(8?qVEq">
                    <field name="CHANNEL">0</field>
                  </block>
                </value>
              </block>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="{0w,3Cn/pvzQ0n*rN?p0">
            <field name="VAR" id="hFT`T6F(Qq$Y*Egy^K31">Ay</field>
            <value name="VALUE">
              <block type="math_arithmetic" id="xILi=kmd/AjiJN^djzel">
                <field name="OP">MULTIPLY</field>
                <comment pinned="false" h="80" w="160">Y is from -1 to 1
        Add 1 to make it 0-2. Multiple by 90 to get 0-180 angle
                    
                </comment>
                <value name="A">
                  <shadow type="math_number" id="BVr_wI9;OKni51rJU?*c">
                    <field name="NUM">90</field>
                  </shadow>
                </value>
                <value name="B">
                  <shadow type="math_number" id="q+Hv0s;T~nshse{y=tec">
                    <field name="NUM">1</field>
                  </shadow>
                  <block type="math_arithmetic" id="5Y{;=6P:nwaeu~._5IAv">
                    <field name="OP">ADD</field>
                    <value name="A">
                      <shadow type="math_number" id="K[k5dKOUEYe-PVuSN_Rt">
                        <field name="NUM">1</field>
                      </shadow>
                    </value>
                    <value name="B">
                      <shadow type="math_number" id="i)1^xR;o3nS|7;]+rU??">
                        <field name="NUM">1</field>
                      </shadow>
                      <block type="get_phone_rotation" id="ClXgZNE[Qii9)_(IlgC`">
                        <field name="CHANNEL">1</field>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="OY[_09P}e#{0cI|zTn~f">
                <field name="VAR" id=",~DkqEJr=3#Az#P/`na3">Az</field>
                <value name="VALUE">
                  <block type="math_arithmetic" id="D(O#X3Eg_U]^!-{LcEnZ">
                    <field name="OP">MULTIPLY</field>
                    <comment pinned="false" h="80" w="160">Z is from -1 to 1. Add 1 to make it 0-2. Multiply by 90 to get 0-180 angle
                        
                        
                    </comment>
                    <value name="A">
                      <shadow type="math_number" id="5?$egk!?C26M:R)[{ivF">
                        <field name="NUM">90</field>
                      </shadow>
                    </value>
                    <value name="B">
                      <shadow type="math_number" id="q+Hv0s;T~nshse{y=tec">
                        <field name="NUM">1</field>
                      </shadow>
                      <block type="math_arithmetic" id="psW4$=ptBRiu.uZ6]#%K">
                        <field name="OP">ADD</field>
                        <value name="A">
                          <shadow type="math_number" id="-!M2Po7+S.*@Z)[$Hfpj">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                        <value name="B">
                          <shadow type="math_number" id="i)1^xR;o3nS|7;]+rU??">
                            <field name="NUM">1</field>
                          </shadow>
                          <block type="get_phone_rotation" id="L%y9uc?D[}`[G`=ee?.+">
                            <field name="CHANNEL">2</field>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="set_PCA9685" id="6;q`HS^KkrF6UF1didh#">
                    <field name="CHANNEL">1</field>
                    <value name="ANGLE">
                      <block type="variables_get" id="Jwy8qR%:YRaZYyiP.JB(">
                        <field name="VAR" id="QB$Sn5l|I2:-d992~yO[">Ax</field>
                      </block>
                    </value>
                    <next>
                      <block type="set_PCA9685" id="PM[QI?afTeT/[Q3-Kz4l">
                        <field name="CHANNEL">2</field>
                        <value name="ANGLE">
                          <block type="variables_get" id="i[@`=uFH1^!fM0{3eL_e">
                            <field name="VAR" id="hFT`T6F(Qq$Y*Egy^K31">Ay</field>
                          </block>
                        </value>
                        <next>
                          <block type="set_PCA9685" id="doiSKQY6cZ!B(da]z=ah">
                            <field name="CHANNEL">3</field>
                            <value name="ANGLE">
                              <block type="variables_get" id="k3{x6`$(1+u10^eB)U*E">
                                <field name="VAR" id=",~DkqEJr=3#Az#P/`na3">Az</field>
                              </block>
                            </value>
                            <next>
                              <block type="plot_datapoint" id="Hb:s?vwdMXqhs4Hg:A3X">
                                <field name="PLOTNAME">myplot</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="A6maBtOK^)#M(05DBIX5">
                                    <field name="VAR" id=",~DkqEJr=3#Az#P/`na3">Az</field>
                                  </block>
                                </value>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
  </block>
<description>
<![CDATA[<h1> Objective  </h1><p><br></p><p> To study control loops where some parameters are measured, and some outputs are set based on the measured inputs. In this demo, we measure the rotation angle of the phone around the X Y and Z axes, and control the motors of a robotic arm using these values. The rotation value can be obtained by the <span style="background-color: rgb(204, 224, 245);">rotation</span> block in the <span style="background-color: rgb(204, 224, 245);">phone</span> menu below the KuttyPy and Sensor Menus. This measures the values from the accelerometer and magnetometer inside the phone to obtain rotation values. </p><p><br></p><h2> Robotic Arm  </h2><p> We need a simple robotic arm with 4 servo motors for this activity. We also need a PCA9685 servo motor controller with an I2C interface. </p><p><br></p><p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4Ss6RXhpZgAASUkqAAgAAAAGABoBBQABAAAAVgAAABsBBQAB
[encoded data, 49203 characters]
U3hiFu3iXq5QMy0TDGe5d9KVGApczabb2RUkG8wTx2gOobply0EaY9ZzLB/F/Lx//9k=">  </p><p><br></p><h2>Rotation to Angle conversion  </h2><p><br></p><p> The rotation measurements are obtained between -1 and 1 . Since angle is between 0 and 180 degrees for each motor, we add 1 to the rotation value in order to make it 0-2 range, and then multiply by 90 to make it 0-180. Therefore, a rotation of 0 will imply a 90 Degree angle set on the corresponding servo motor.</p><p><br></p><ol><li> The X axis rotation is scaled, and copied to the pincer servo.</li><li> The Y axis rotation controls the extender servo of the robotic arm.</li><li> The Z axis rotation controls the rotation of the robotic arm.  The SG-90 Servo  </li></ol><p><br></p><p> This is the SG-90 servo motor. It has 3 connections. Vcc(5V), GND, and a PWM input. For this activity, we are using the PCA9685 PWM generator, but we can also use SQ1 or SQ2 to generate the necessary signals to drive this servo. It can consume a lot of current, so please connect a separate power supply of 5V to drive this motor. </p><p><img src="
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAABcCAYAAADj79JYAAAABHNCSVQICAgIfAhkiAAAIABJREFU
[encoded data, 19327 characters]
AABJRU5ErkJggg==
"> </p><p><br></p><h3> Plotting </h3><p><br></p><p> We also plot the rotation around the X axis as a function of time just for visualization purposes. </p><p> </p><p> </p><iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/q1EV9xA5IPY?si=W3aTX02a5mMLCr4z"></iframe><p><br></p>]]>
</description>
</xml>
