====== Components ====== In the sBotics simulator, "components" refers to blocks with special behaviors that can be controlled through programming. These components allow robots to interact with the virtual environment and perform complex tasks. ===== Types of Components ===== Components are divided into two main categories: * [[#Simple_Components|Simple Components]] * [[#Sensors|Sensors]] ===== Naming ===== Every accessible component has a name, which serves as an access key. In traditional robots, the programmer has to call a function like ''color(1)'' and then figure out which robot sensor corresponds to the sensor on port number ''1''. In sBotics, you name your own sensor, so you can call your color sensors as LeftColorSensor and RightColorSensor, and thus the code can be executed as ''Color("LeftColorSensor")'' or ''Color("RightColorSensor")''. As you can see, these names are **EXTREMELY** important for accessing components and issuing commands in programming. These names are given in the [[Guide:Understanding Construction|"Robot Workshop"]], by clicking on a component and changing the name in the field that appears on the right and clicking the change button, as shown in the images below. {{ alterarnome.png?200 }} {{ alterarnomebotao.png?200 }} {{ edited_successfully.png?600 }} > **Attention:** On some computers, the change button's activation is slightly off-center, so when clicking the button, verify that the "Edited Successfully!" message appears at the top of the screen. If it doesn't appear initially, simply press a less central part of the button. To see the names of your robot's components, simply go to the [[Robots]] screen and view them at the top of this menu: {{ components.png?600 }} ----------------------- {{ controller-white.png?150}} ===== Robot Controller ===== The robot controller can be considered a component by many, as it is capable of executing some commands from "embedded" sensors and outputs. {{controller.png?20}} **Controller Commands:** | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Open Console | Opens the console panel. | ''%%OpenConsole()%%'' | ''%%IO.OpenConsole();%%'' | ^ Write | Writes to the console immediately after the last text. | ''%%Write("text")%%'' | ''%%IO.Print("text" );%%'' | ^ Write Line | Writes to the console on a new line. | ''%%WriteLine("text")%%'' | ''%%IO.PrintLine("text");%%'' | ^ Clear Console | Clears all text from the console. | ''%%ClearConsole()%%'' | ''%%IO.ClearPrint();%%'' | ^ Compass / Direction | Returns the direction (in degrees) observed by the robot. | ''%%Direction()%%'' | ''%%Bot.Compass%%'' | ^ Inclination | Returns a number indicating the robot's inclination in degrees. | ''%%Inclination()%%'' | ''%%Bot.Inclination%%'' | ^ Speed | Returns a number indicating the speed at which the controller (robot as a whole) is moving. | ''%%Speed()%%'' | ''%%Bot.Speed%%'' | //(Advanced)// Log File: | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Log | Writes to the log immediately after the last text. | ''%%Log("text")%%'' | ''%%IO.Write("text" );%%'' | ^ Log Line | Writes to the log on a new line. | ''%%LogLine("text")%%'' | ''%%IO.WriteLine("text");%%'' | ^ Clear Log | Clears all text from the log file. | ''%%ClearLog()%%'' | ''%%IO.ClearWrite();%%'' | > The log file is like a console but resides entirely outside sBotics in a separate file. It can be accessed through the [[Routine#Console|console panel]] by clicking on the "Document" icon. It can be used to generate graphs, CSVs, and other implementations the user wants to do outside sBotics from data generated within sBotics. ===== Simple Components ===== Simple components are blocks that perform specific actions when programmed. sBotics offers the following simple components: {{ components2.png?300 }} * {{p-buzzer.png?20}} **Buzzer:** Plays sounds, noise, or musical notes. * {{p-led.png?20}} **LED:** Emits light in a specific color. * {{p-servomotor.png?20}} **Servomotor:** Rotates a [[robots#physics_and_connectors|block group]] at a defined angle. * {{p-pen.png?20}} **3D Pen:** Draws lines along the robot's path. ==== Understanding the Servomotor Better ==== Although the servomotor may seem like a "simple" component, it is the most important component of any robot, as it allows for programmed movement of [[robots#physics_and_connectors|block groups]] and has many forms of interaction. {{ motor_spinning.gif }} > **EVERY MOTOR STARTS LOCKED, and it is necessary to UNLOCK THE MOTOR to use it.** > //If using multiple motors, don't forget to unlock them individually to use them.// > ''%%LockMotor("motor1", false)%%'' > ''%%LockMotor("motor2", false)%%'' > ''%%LockMotor("motor3", false)%%'' > ''%%LockMotor("motor4", false)%%'' Motors can be locked or unlocked. A locked motor behaves somewhat like a solid block and creates drag/friction and can be used to make turns (one locked and one unlocked motor, for example). A loose motor can move freely, even if no force is being applied to it, and can receive commands for movement. > {{motor_debugging.png?500}} > //By selecting a generic motor in the [[Debugging]] menu, you can play with each of its properties.// Motors in sBotics move with two properties: **Force** and **Target Speed**. For 90% of users, this information is irrelevant; simply use the command ''%%Move("motor", force)%%'' in rEduc / Blockeduc to move the motor. Motors can move with speeds from -500 to 500, with the direction determined by whether the number is positive or negative. > **Advanced:** For more advanced users, simply understand that target speed is the most important, as it is the speed the motor will reach, being positive (rotating in the direction of the arrows) or negative (rotating against the arrows); Force is the force to be applied every 1/60 seconds to achieve the desired "target speed," thus only positive numbers. > > //For this reason, in C#, movement is always given by two values, one being the force applied and the other the speed to be reached. If you don't want to deal with this, you can always use the same value in both fields, but remember that the first parameter **must always be positive**// > ''%%Bot.GetComponent("Motor Name").Apply(100, -500)%%'' ==== Commands for Simple Components ==== As each simple component has unique functions, their commands vary. Refer to the table below for detailed information about each component's commands. > **Remember!** To access a component via a command, you must provide its name. Ensure a name has been entered in the Robot Workshop and verify the component's name in the **[[Robots]]** panel. **{{p-buzzer.png?20}} Buzzer:** | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Is Playing? | Returns whether the specified buzzer is playing something or not (true/false). | ''%%IsPlaying("Name")%%'' | ''%%Bot.GetComponent( "Name" ).Playing %%'' | ^ Play Note | Plays a musical note on the specified buzzer. | ''%%PlayNote("Name", "G")%%'' | ''%%Bot.GetComponent( "Name" ).PlaySound("G");%%'' | ^ Play Frequency | Plays a specific frequency in Hz on the specified buzzer. | ''%%PlayFrequency("Name", 24.5)%%'' | ''%%Bot.GetComponent( "Name" ).PlaySound( 24.5 );%%'' | ^ Stop Sound | Deactivates the specified buzzer. | ''%%StopSound("Name")%%'' | ''%%Bot.GetComponent( "Name" ).StopSound();%%'' | **{{p-led.png?20}} LED Light:** | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Turn On | Sets the color of the specified LED and turns it on. | ''%%TurnOnLight("Name", 255, 255, 255)%%'' | ''%%Bot.GetComponent( "Name" ).TurnOn( new Color(255, 255, 255) );%%'' | ^ Turn Off | Turns off the specified LED. | ''%%TurnOffLight("Name")%%'' | ''%%Bot.GetComponent( "Name" ).TurnOff();%%'' | **{{p-servomotor.png?20}} Servomotor:** | Basic Commands ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Lock / Unlock Motor | Locks or Unlocks the motor from moving. | ''%%LockMotor("Motor Name", false)%%'' | ''%%Bot.GetComponent("Motor Name").Locked = false;%%'' | ^ Move Motor | Moves the **unlocked** servomotor at the specified speed between -500 and 500. | ''%%Motor("Motor Name", -500)%%'' | ''%%Bot.GetComponent("Motor Name").Apply(10, -50)%%'' | | //Advanced Commands// ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Set Motor | Gradually applies a given force to reach the other specified speed. | ''%%SetMotor("Motor Name", 10, -500)%%'' | ''%%Bot.GetComponent("Motor Name").Apply(10, -50)%%'' | ^ Motor Angle* | Returns the angle of the specified motor. | ''%%MotorAngle("Motor Name")%%'' | ''%%Bot.GetComponent("Motor Name").Angle%%'' | ^ Motor Force | Returns the force being applied to the specified motor. | ''%%MotorForce("Motor Name")%%'' | ''%%Bot.GetComponent("Motor Name").Force%%'' | ^ Motor Target Speed | Returns the target speed to be reached by the specified motor. | ''%%MotorTargetSpeed("Motor Name")%%'' | ''%%Bot.GetComponent("Motor Name").Target%%'' | ^ Motor Speed | Returns the speed (acceleration) of the specified motor. | ''%%MotorSpeed("Motor Name")%%'' | ''%%Bot.GetComponent("Motor Name").Velocity%%'' | > * The sBotics angular (gyroscopic) sensor function can sometimes exhibit a known error due to limitations in the Unity platform (the platform used to create the simulator). The error manifests as difficulty correctly interpreting object movement on other axes, affecting the reading of the specific desired axis. > > Alternative solutions can be explored, such as repositioning the motor and rotating it in different ways (by clicking the block rotation buttons in the [[guide:understanding_construction|"Robot Workshop"]]), attempting seemingly identical rotations with different values, or changing the block where the motor is anchored. Correcting this issue is currently unfeasible. **{{p-pen.png?20}} 3D Pen:** | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Turn On Pen | Sets the color of the specified pen and turns it on. | ''%%TurnOnPen("Name", 255, 255, 255)%%'' | ''%%Bot.GetComponent( "Name" ).TurnOn( new Color(255, 255, 255) );%%'' | ^ Erase Pen | Erases everything drawn with the specified pen. | ''%%ErasePen("Name")%%'' | ''%%Bot.GetComponent( "Name" ).Clear();%%'' | ^ Turn Off Pen | Turns off the specified pen. | ''%%TurnOffPen("Name")%%'' | ''%%Bot.GetComponent( "Name" ).TurnOff();%%'' | [[https://code.sbotics.net/programming/platforms/c0db56ec-619f-498f-9fbb-7835735f0f5e/languages/4224cf8e-f00c-4cc5-bd08-f0d22fad011d/functions|View All Commands and Detailed Explanations]] ----------------------- ===== Sensors ===== Sensors allow robots to "perceive" the virtual environment, collecting information about colors, distances, touches, and images. sBotics offers the following sensors: {{ sensors.png?300 }} * {{p-color-sensor.png?20}} **Color Sensor:** Detects the color of a surface. * {{p-ultrasonic.png?20}} **Ultrasonic Sensor:** Measures the distance to an object. * {{p-touch.png?20}} **Touch Sensor:** Detects if an object is in contact with the sensor. * {{p-camera.png?20}} **Camera:** Captures images of the environment (incomplete functionality). **Note:** Color and ultrasonic sensors also have angled versions, which allow reading different areas. ===== Sensor Readings ===== Each sensor offers two types of readings: **Analog reading** and **digital reading**. For rEduc and Blockeduc users, this separation is not as obvious, as they are all handled by distinct commands. > **Remember!** To access a component via a command, you must provide its name. Ensure a name has been entered in the Robot Workshop and verify the component's name in the **[[Robots]]** panel. ----------------------- ==== Analog Reading: ==== Returns a numerical value representing the intensity of the reading (can be a numerical value, text, or other*). **{{p-color-sensor.png?20}} Color Sensor:** Variable of type **//color*//** (C#) or **number**/**text** (rEduc/Blockeduc). | Commands ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Color | Returns the //"color name"*//* read by the specified sensor. | ''%%Color("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog.ToString()%%'' | ^ Brightness | Returns the numerical value representing the light intensity (black-and-white reading) of the specified sensor. | ''%%Light("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog.Brightness%%'' | ^ Blue Tone | Returns the numerical value of blue of the color read by the specified sensor. | ''%%BlueColor("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog.Blue%%'' | ^ Green Tone | Returns the numerical value of green of the color read by the specified sensor. | ''%%GreenColor("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog.Green%%'' | ^ Red Tone | Returns the numerical value of red of the color read by the specified sensor. | ''%%RedColor("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog.Red%%'' | ** {{p-ultrasonic.png?20}} Ultrasonic Sensor:** Measures the distance to an object. | Commands ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Ultrasonic | Returns the distance (in the simulator's own units) perceived by the specified sensor. | ''%%Ultra("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Analog%%'' | ** {{p-touch.png?20}}Touch Sensor:** Touch sensor //does not have analog reading, only digital, as it is a simple button//. > //* Other return types only apply to C#// > *//* The color types that exist in sBotics are:// > **rEduc / Blockeduc:** ''%%"Black"%%'', ''%%"White"%%'', ''%%"Green"%%'', ''%%"Red"%%'', ''%%"Blue"%%'', ''%%"Yellow"%%'', ''%%"Magenta"%%'', ''%%"Cyan"%%''. > **C#:** ''%%Colors.Black%%'', ''%%Colors.White%%'', ''%%Colors.Green%%'', ''%%Colors.Red%%'', ''%%Colors.Blue%%'', ''%%Colors.Yellow%%'', ''%%Colors.Magenta%%'', ''%%Colors.Cyan%%''. ----------------------- ==== Digital Reading: ==== Returns a boolean value (true/false) indicating whether a condition has been met. **{{p-color-sensor.png?20}} Color Sensor:** //Boolean// variable (true or false) that indicates whether the sensor is reading any color or not (black). | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Colored? | Returns whether the specified sensor is reading any color. | ''%%IsColored("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Digital;%%'' | ** {{p-ultrasonic.png?20}} Ultrasonic Sensor:** Measures the distance to an object. | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Has Something? | Returns whether the specified sensor is seeing anything, regardless of distance. | ''%%HasSomething("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Digital;%%'' | ** {{p-touch.png?20}}Touch Sensor:** Detects if an object is in contact with the sensor. | Command ^ Description ^ rEduc / Blockeduc ^ C# ^ ^ Touched? | Returns whether the specified sensor is being pressed. | ''%%IsTouched("Sensor Name")%%'' | ''%%Bot.GetComponent( "Sensor Name" ).Digital;%%'' | //**Important:** The camera is not yet fully functional and may not be usable in most cases. It has C# commands that can be viewed on the [[https://github.com/sBotics/programming-reference|C# Programming Reference]] page, but it is not on the wiki due to lack of support.//