port = "\\\\.\\COM4"
function sysCall_init()
sim = require('sim')
serialHandle = sim.serialOpen(port, 9600)
print(serialHandle)
pivot = sim.getObject('/Pivot')
motor = sim.getObject('/Motor')
end
function sysCall_actuation()
pivotPosition = sim.getJointPosition(pivot)
sim.serialSend(serialHandle, tostring(pivotPosition))
target = sim.serialRead(serialHandle, 32, '\n');
sim.setJointTargetForce(motor, -target, true)
end
My handling of the serial data may be incorrect, however my issue lies with the opening of the serial communication, the handle returned by sim.serialOpen is -1 no matter what I do. COM4 is the port that the Arduino IDE is using successfully to upload sketches to the board.
yFleury wrote: ↑19 Jul 2024, 19:23
My goal is to use serial communication between CoppeliaSim and Arduino to manipulate simulation elements through sensors read by the Arduino.
port = "\\\\.\\COM4"
function sysCall_init()
sim = require('sim')
serialHandle = sim.serialOpen(port, 9600)
print(serialHandle)
pivot = sim.getObject('/Pivot')
motor = sim.getObject('/Motor')
end
function sysCall_actuation()
pivotPosition = sim.getJointPosition(pivot)
sim.serialSend(serialHandle, tostring(pivotPosition))
target = sim.serialRead(serialHandle, 32, '\n');
sim.setJointTargetForce(motor, -target, true)
end
My handling of the serial data may be incorrect, however my issue lies with the opening of the serial communication, the handle returned by sim.serialOpen is -1 no matter what I do. COM4 is the port that the Arduino IDE is using successfully to upload sketches to the board.
I found my solution luckily. The problem for me is that I ran the Coppelia Sim script with the Arduino IDE serial monitor opening. So the port was detected as "is using". Please make sure that the port you selected in Coppelia Sim is not using in other software.
Tinayou wrote: ↑01 Aug 2024, 10:09
I found my solution luckily. The problem for me is that I ran the Coppelia Sim script with the Arduino IDE serial monitor opening. So the port was detected as "is using". Please make sure that the port you selected in Coppelia Sim is not using in other software.
That was my guess, but then how do I upload the script to the arduino? Ulpload, close the IDE and run Coppelia?
I only use the Arduino transmits data from outside source to the PC port, and the script inside CoppeliaSim read and process data. So after I download the Arduino code into the chip, the Arduino IDE only is used to monitor the data to the port. Closing it would not bring any affect to the system. Maybe our application scenarios are not exactly the same. Hope it would help you.
As long as its open Coppelia won't comunicate via serial.
If you need to see what serial data is being passed in between the microcontroler and the simulation like I do, use something like Serial Port Monitor.