In this article, we will provide guidelines for creating MicroBasic scripts utilizing a Generative AI engine. The AI engine used will be determinant for the script results, since various AI models have different capabilities in understanding and achieving human-like reasoning. To facilitate script development, we have created a prompt that can be provided to the AI module and describes MicroBasic syntax, operations, and supported functions for the controller. Due to prompt length limitations, only the essential commands, queries, and configurations are provided. Consequently, if the required command is missing from the prompt, the generated script will include some pseudo-command names, which should be replaced with the actual command names from the dictionary.


It's important to note that generative AI is prone to logical errors and has not been trained specifically in the MicroBasic scripting language. As such, the generated scripts are not guaranteed to be functional. Users must validate and test the scripts at their own risk. If the provided script does not meet your requirements, additional instructions can be supplied to the AI model for corrections. The more precise the given prompt, the more likely the generated script will be correct. Always review the script and ask generative AI  to make the necessary corrections before trying to compile it.


Disclaimer


Roboteq is not liable for any errors, omissions, or adverse effects that may result from the use or implementation of the AI generated script. It is the user's responsibility to extensively test and validate the script in all intended operational environments. Use at your own risk.



Copy-pasting the prompt


One way to provide instructions is by copy-pasting the prompt that is provided at the end of this article. The prompt ends with the following sentence:


"...Compose a microBasic script that…"


Copy-paste the the complete prompt and add your script instructions after this line.


E.g:


"...Compose a microBasic script that… Enables the digital output where the motor brake is connected when the motor moves. Disable the output 500 ms after the motor has stopped."


The final prompt should consist of the MicroBasic language description and you prompt:


Prompt = [microbasic language description] + [your prompt]



Using the Browser extension


We have created a public prompt using the AIPRM extension. To utilize it, first install the AIPRM plugin on your browser. Then, search for the MicroBasic Scripting Extension. By selecting this extension, you'll be able to input script instructions directly, without the need to copy-paste the prompt.



Script examples


Let's look into the below example with the provided prompt:


"Operates in Closed Loop Speed mode and takes motor commands through a user var. Enables a digital output where the motor brake is connected when the motor moves. Disables the output 500 ms after the motor has stopped."


The script follows:


 Option Explicit

 

Dim speedSetPoint As Integer

Dim speedActual As Integer

Dim outputTimer As Integer

Dim motorChannel As Integer

 

' Initialize variables

speedSetPoint 0

speedActual 0

outputTimer 0

motorChannel 1  ' Assuming motor 1

 

While 1  ' Main loop

    ' Read the speed setpoint from a user variable (replace VAR with the actual user variable identifier)

    speedSetPoint GetValue(_VAR1)

    

    ' Set the speed mode and the speed setpoint for the motor


    SetConfig(_MMODmotorChannel1)

    SetCommand(_SmotorChannelspeedSetPoint)

    

    ' Read the actual speed of the motor

    speedActual GetValue(_SmotorChannel)

    

    ' Control the digital output for the motor brake

    If Abs(speedActual) > Then

        ' Motor is moving, enable the brake digital output (replace D1 with the actual digital output identifier)

        SetCommand(_D11)

        

        ' Reset the output timer

        outputTimer 0

    Else

        ' Motor has stopped, start or continue the timer

        outputTimer outputTimer 1

        

        ' If timer has reached 50 (500 ms / 10 ms), disable the brake digital output

        If outputTimer >= 50 Then

            SetCommand(_D01)

        End If

    End If

    

    Wait(10)  ' 10 ms wait

    

End While

 

We notice the following mistakes/omissions:


1. The operating mode is constanty chagning inside the loop which ideally should be avoided for configuration commands

2. It reads the _S parameter to get the motor speed, although this is applicable only when encoder is used. _F parameter            should be used instead, which returns the motor feedback with any sensor

3. The script checks the motor speed to release the brake, although the motor cannot run until the brake is released

4. With some sensors that are sensitive to noise, the motor speed might never be constantly zero. We will add a threshold in

    the motor speed to engage the brake.


Besides these issues, the script logic is correct. We will modify the script by giving the following prompt:


"Set the Operating mode only once and not in the loop. Use the feedback parameter to read the motor speed. Release the brake when a motor command is given and not when there is motor speed. Use a threshold of 10 RPM in the motor speed to engage the brake."


The generated script follows: 


Option Explicit

 

Dim speedSetPoint As Integer

Dim speedActual As Integer

Dim outputTimer As Integer

Dim motorChannel As Integer

 

' Initialize variables

speedSetPoint 0

speedActual 0

outputTimer 0

motorChannel 1  ' Assuming motor 1

 

' Set the operating mode to Closed Loop Speed mode only once

SetConfig(_MMODmotorChannel1)

 

While 1  ' Main loop

    ' Read the speed setpoint from a user variable (replace VAR with the actual user variable identifier)

    speedSetPoint GetValue(_VAR1)

    

    ' Set the speed setpoint for the motor

    SetCommand(_SmotorChannelspeedSetPoint)

    

    ' Read the actual speed of the motor from the feedback parameter

    speedActual GetValue(_FmotorChannel)

    

    ' Control the digital output for the motor brake

    If speedSetPoint <> Then

        ' A motor command is given, release the brake (replace D0 with the actual digital output identifier)

         SetCommand(_D01)

        

        ' Reset the output timer

        outputTimer 0

    ElseIf Abs(speedActual) < 10 Then

        ' Motor has stopped or is below the 10 RPM threshold, start or continue the timer

        outputTimer outputTimer 1

        

        ' If timer has reached 50 (500 ms / 10 ms), engage the brake (replace D1 with the actual digital output identifier)

        If outputTimer >= 50 Then

             SetCommand(_D11)

        End If

    Else

        ' Reset the output timer if the motor speed is above 10 RPM

        outputTimer 0

    End If

    

    Wait(10)  ' 10 ms wait

End While


This time the script has inverted the brake logic, considering that the brake is released when the digita output is zero, which is not the case for most solenoids that are being controlled with open collector outputs. So we will need to ask it to invert the output logic:


"Invert the digital output logic. The brake is disengaged when the output is high."


We see that the generated script has the requested modifications by considering also the previous prompts:


 

Option Explicit

 

Dim speedSetPoint As Integer

Dim speedActual As Integer

Dim outputTimer As Integer

Dim motorChannel As Integer

 

' Initialize variables

speedSetPoint 0

speedActual 0

outputTimer 0

motorChannel 1  ' Assuming motor 1

 

' Set the operating mode to Closed Loop Speed mode only once

SetConfig(_MMODmotorChannel1)

 

While 1  ' Main loop

    ' Read the speed setpoint from a user variable (replace VAR with the actual user variable identifier)

    speedSetPoint GetValue(_VAR1)

    

    ' Set the speed setpoint for the motor

    SetCommand(_SmotorChannelspeedSetPoint)

    

    ' Read the actual speed of the motor from the feedback parameter

    speedActual GetValue(_FmotorChannel)

    

    ' Control the digital output for the motor brake

    If speedSetPoint <> Then

        ' A motor command is given, disengage the brake by setting the output high (replace D0 with the actual digital output identifier)

        SetCommand(_D11)

        

        ' Reset the output timer

        outputTimer 0

    ElseIf Abs(speedActual) < 10 Then

        ' Motor has stopped or is below the 10 RPM threshold, start or continue the timer

        outputTimer outputTimer 1

        

        ' If timer has reached 50 (500 ms / 10 ms), engage the brake by setting the output low (replace D1 with the actual digital output identifier)

        If outputTimer >= 50 Then

            SetCommand(_D01)

        End If

    Else

        ' Reset the output timer if the motor speed is above 10 RPM

        outputTimer 0

    End If

    

    Wait(10)  ' 10 ms wait

End While

 

From this example we can conclude that the generative AI is prone to logical mistakes and gives inconsistent results. Because of this, it should be used only as a reference and one should not assume that it can provide a final, error-free script. You can read below the generative AI's reply when it got asked about that topic: 


"You are correct that the code snippets provided here should be used primarily as references and should undergo rigorous testing before being considered as final implementations. My aim is to assist and provide guidance, but the onus of ensuring the functionality and safety of any code still rests with the human programmer or engineer.


The inconsistency you pointed out—such as referring to different digital outputs (_D0 and _D1) for engaging and disengaging the brake—is an oversight and certainly something to be cautious about when using generated code as a starting point.


Thank you for your feedback, and I apologize for any errors or inconsistencies."

 

Manual prompt


The Prompt for generative AI follows:



MicroBasic: Like Basic but:

1.    For var=value AndWhile cond...Next

2.    while(cond)...end while

3.    Declare vars: dim var as integer/boolean

4.    Start: option explicit

5.    Declare vars first

6.    No line nums

7.    End: terminate

8.    Jump: goto label

9.    Subs: gosub label...return

10.    Comments: '

11.    Ifs: if cond...else...end if (+ esleif)

12.    Arrays: [index]

13.    Inc: var++

14.    32-bit int, bool only

15.    Print: print(var, "txt"), \n \t

16.    Delay: wait(ms)

17.    Modulo: Mod

18.    Logic ops: And, Or, etc.

19.    Bit: 0BXXXX

20.    Hex: 0xXXXX

21.    Eq: =, Ineq: <>

Math funcs: : Abs(), Atan(), Cos(), Sin(), Sqrt().


The language supports the following built-in functions to send motor commands, queries and access (get and set) the controller configurations:

1.     SetCommand(_CMD,cc,nn) is used to send a runtime command

2.    GetVlaue(_CMD,cc) is used to send a runtime query

3.    SetConfig(_CMD,cc,nn) is used to set a configuration value

4.    GetConfig(_CMD,cc) is used to get a configuration value

CMD must be replaced with the command to send (runtime command, query, or configuration), and underscore should be left as it is. cc corresponds to the channel (motor 1 or motor 2) and nn is the value. Note that getVlaue and getConfig do not have a nn argument since they return that value. The value can be assigned directly it to a script variable. The following example reads the motor amps of channel 1 and assigns them to the motorCurrent variable.

motorCurrent = getvalue(_A,1)

The following Runtime commands are supported through setCommand(_CMD,cc,nn) function: 

•           VAR: set an integer user variable. User can interact through user vars from console

•    C: Change feedback counter values

•    D1: Set a digital output (has only cc argument)

•    D0: Reset a digital output (has only cc argument)

•    EX: Set emergency stop (supports only cc = 1)

•    MG: Release emergency stop (supports only cc = 1)

•    G: Send motor commands in Open Loop. Range is -1000 to +1000 and corresponds to motor voltage percentage x 10

•    S: Send motor commands in Speed mode. value in RPM.

•    P: Send motor commands in Position mode. Value in counts

•    GIQ: Send a Torque amps command (AKA Iq or quadrature amps) in Torque mode. Value is amps x 10: GIQ. 

•    GID: Send a Flux amps command (AKA Id) in Torque mode. Value is ampsx10: 

•    SFT: Give a safety stop (where the motor decelerates with a pre-defined deceleration)


The following runtime queries are supported through the GetValue(_CMD, cc) function. 

•    A: Get the motor rms current. Value is amps x 10

•    MA: Get the motor Id current (AKA flux amps) and Iq current (AKA torque or quadrature amps) current for both channels. Value is amps x 10. Cc = 1 gives Flux amps of channel 1, Cc = 2 gives Torque amps of channel 1, Cc = 3 gives Flux amps of channel 2, Cc = 4 gives Torque amps of channel 2

•    PHA: Get the phase amps of both motors that correspond to the phase instantaneous current. Value is amps x 10. cc =1 gives channel 1 phase U, cc =2 gives channel 1 phase V, cc =3 gives channel 1 phase W, cc =4 gives channel 2 phase U, cc =5 gives channel 2 phase V, cc =6 gives channel 2 phase W 

•    AI: Read Analog Input. Range is 0 to 5000, value is in milliVolts

•    DI: Read Digital input

•    ANG: Get the rotor angle. O to 360 degrees are mapped to 0 to 511


•        VAR: Read the value of an integer user var. User can interact through user vars from console

•    BA: Read the battery current in amps x 10

•    S: Read the motor speed in RPM

•    C: Read the encoder counter

•    DO: Read the digital outputs state. It is a binary value with the state of all 4 outputs with 1 being the LSB.

•    DR: Check if the destination in position mode is reached. 

•    E: Get the Closed Loop error (set value minus the feedback). This is RPM in speed, counts in position mode and ampsx10 in torque mode

•    F: Get the feedback value. This is RPM in speed, counts in position mode and ampsx10 in torque mode

•    FF: Get the controller fault flags (overheat, overvolt, undervolt, short, Emegency stop, motor/sensor fault, MOSFET failure, STO fault). Each flag corresponds to a bit with the mentioned sequence. Overheat is the LSB

•    FS: Get the controller status flags (command mode, power stage disabled, stall, limit switch, STO triggered, script running,  motor/sensor setup taking place). Each flag corresponds to a bit with the mentioned sequence. Command mode is the LSB.

•    FM: Get the motor status flags (amp limit, stall, Loop error, safety stop, fwd limit switch, rev limit switch, amps trigger, fets off). Each flag corresponds to a bit with the mentioned sequence. Amp limit is the LSB

•    HS: Get the hall sensor state. This gets a value from 1 to 6 depending on the 3 hall sensors state.

•    M: Read the issued motor command (for both Speed, Position and Torque mode). This is the command issued by the user

•    P: Read the applied motor voltage.  Value is percentage x 10. 

•    RMP: Get the motor’s trajectory in Open Loop, Speed and Torque mode. This is the value that the motor is expected to have over time. In Speed mode the value is expressed in RPM, In Torque mode, is ampsx10 and  in Open Loop is PWM duty cycle x 10 (100 % is 1000). 

•    TR: Get the motor’s trajectory in Position mode. This is the position that the motor is expected to have over time.  

•    T: Get the controller temperature (cc = 1:MCU, cc = 2: Heatsink 1, cc = 3: Heatsink2, cc = 4: Motor 1, cc = 5: Motor 2).

•    V: Get the controller voltages. Cc = 1 is the MOSFET driver's voltage (AKA internal volts), cc=2 is battery voltage (AKA Vmot voltage) and cc = 3 is 5V output voltage. Internal volts and battery volts are reported in volts x 10 and 5 volts output in millivolts.

The following configurations can be get and set with setConfig(_CMD,cc,nn) and getConfig(_CMD,cc) functions.

•    ALIM: Current limit value. The controller will not allow the current to exceed this value. Value is expressed in amps x 10.

•    MAC: The motor acceleration rate. Values in RPM x 10/sec.

•    MDEC: The motor deceleration rate.Values in RPM x 10/sec.

•    KIG: The PID integral gain for Speed and Position Loops for each channel. cc = 1 is for Speed Integral Gain for motor 1, cc = 2 is for Speed Integral Gain for motor 2 cc = 3 is for Position Integral Gain for motor 1, cc = 4 is for Position Integral Gain for motor 2. Values multiplied by 1000000.

•    KIP: The PID proportional gain. for Speed and Position Loops for each channel. cc = 1 is for Speed Proportional Gain for motor 1, cc = 2 is for Speed Proportional Gain for motor 2 cc = 3 is for Position Proportional Gain for motor 1, cc = 4 is for Position Proportional Gain for motor 2. Values multiplied by 1000000.

•    MMOD: The operating mode of the controller. Nn = 0 is for Open-loop, Nn = 1 is for Closed-loop speed, Nn = 2 is for Closed-loop position relative, Nn = 3 is for Closed-loop count position, Nn = 4 is for Closed-loop position tracking, Nn =5 is for Closed-loop torque, Nn = 6 is for Closed-loop speed position.

•    MVEL: The motor speed in position mode. 

•    KIF: The integral gains for the FOC loop (AKA torque loop) for the Flux current (AKA Id current) and Torque current (AKA Iq current) for both channels. Cc = 1 is Flux Integral Gain for motor 1, cc = 2 is Flux Integral Gain for motor 2, cc =3 is for Torque Integral Gain for motor 1 and cc = 4 is for Torque Integral Gain for motor 2. Values are x 1000000 for F3 products and x10000 for G4.

•    KPF: The proportional gains for the FOC loop (AKA torque loop) for the Flux current (AKA Id current) and Torque current (AKA Iq current) for both channels. Cc = 1 is Flux Proportional Gain for motor 1, cc = 2 is Flux Proportional Gain for motor 2, cc =3 is for Torque Proportional Gain for motor 1 and cc = 4 is for Torque Proportional Gain for motor 2. Values are x 1000000 for F3 products and x10000 for G4.

•    TID: The set point for the Flux current. Value is Amps x 10.

Script Rules:

•    Use while 1 main loop.

•    Use defined cmds only.

•    No floats; use multipliers.

•    10ms loop: read inputs, process, update outputs.

•    No blocking code, no blocking delays; Use counters and the 10 ms wait to implement bigger delays.

•    Always wait() in loop.

•    If cmd not listed, comment as placeholder.


Compose a microBasic script that…



Add youscript instructions here